User Tools

Site Tools


docker:containers:run_a_docker_container

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
docker:containers:run_a_docker_container [2020/11/11 11:50] 192.168.1.1docker:containers:run_a_docker_container [2020/11/11 12:12] (current) 192.168.1.1
Line 70: Line 70:
 ---- ----
  
-===== Example =====+===== Run a Container and Publish Container Ports =====
  
 +<code bash>
 +docker container run --name docker-nginx -d -p 8080:80 nginx
 +</code>
  
-Launching a container is simple as **docker run** + the image name you would like to run + the command to run within the container.  +where:
  
-If the image doesn't exist on your local machine, Docker will attempt to fetch it from the public image registry.  +  * **8080** is the port of the Docker host. 
 +  * **80** is the port of the container where the application is running. 
 + 
 +<WRAP info> 
 +**NOTE:**  You can verify the port 8080 using the URL http://docker-host-ip:8080 in your web browser. 
 + 
 +An optional host-ip can also be set: 
 + 
 +<code bash> 
 +docker container run -p [host-ip]:[host-port]:[container-port] 
 +</code> 
 + 
 +</WRAP> 
 + 
 + 
 +---- 
 + 
 +===== Remove the Container After Exit ===== 
 + 
 +<code bash> 
 +docker container run --rm mongo 
 +</code> 
 + 
 +---- 
 + 
 +===== Run a Container and Mount Host Volumes ===== 
 + 
 +By default, Docker containers do not save the data. When a container is stopped and the process is completed, all data generated by the container is removed. 
 + 
 +Sometimes you may need to run a container that requires persistent storage. In that case, you can use Docker volume to make the data persist and also share it across the multiple containers. 
 + 
 +<code bash> 
 +docker container run --name docker-nginx -d -p 8080:80 -v /mnt:/var/www nginx 
 +</code> 
 + 
 +<WRAP info> 
 +**NOTE:**  Any data written to **/var/www** within the container is actually accessing **/mnt** on the host. 
 + 
 +This option is useful for anyone running a database or application that requires persistence within Docker. 
 + 
 +</WRAP> 
 + 
 + 
 +---- 
 + 
 +===== Example =====
  
 For example, if you ran **/bin/echo hello world** as your command, the container will start, print hello world and then stop: For example, if you ran **/bin/echo hello world** as your command, the container will start, print hello world and then stop:
docker/containers/run_a_docker_container.1605095400.txt.gz · Last modified: 2020/11/11 11:50 by 192.168.1.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki