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

Next revision
Previous revision
docker:containers:run_a_docker_container [2020/05/13 08:44] – created peterdocker:containers:run_a_docker_container [2020/11/11 12:12] (current) 192.168.1.1
Line 4: Line 4:
  
 If the image doesn't exist on your local machine, Docker will attempt to fetch it from the public image registry.   If the image doesn't exist on your local machine, Docker will attempt to fetch it from the public image registry.  
 +
 +Containers can be run in different modes:
 +
 +  * **Attached mode**:  The default mode.  The container is  attached to the terminal session and output of the process will be displayed on your terminal.
 +  * **Detached mode**:  Runs the process in the background, and keeps the container running after exiting from the terminal session.
 +  * **Interactive mode**:  Allows you to execute commands inside the container that is still running.
 +
 +----
 +
 +===== Run a Container in Attached Mode =====
 +
 +<code bash>
 +docker container run nginx
 +</code>
 +
 +<WRAP info>
 +**NOTE:**  You can press **CTRL + C** to stop the running Nginx container.
 +</WRAP>
 +
 +
 +----
 +
 +===== Run a Container Under a Specific Name =====
 +
 +<code bash>
 +docker container run --name docker-nginx nginx
 +</code>
 +
 +where:
 +
 +  * **docker-nginx** is the name of the container.
 +  * **nginx** is the name of the Docker image that Docker will download from the Docker Hub registry.
 +
 +----
 +
 +===== Run a Container in Detached Mode =====
 +
 +Runs the container in the background.
 +
 +<code bash>
 +docker container run --name docker-nginx -d nginx
 +</code>
 +
 +Verify the running container:
 +
 +<code bash>
 +docker ps
 +</code>
 +
 +returns:
 +
 +<code bash>
 +CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
 +0bb07d011cc9        nginx               "/docker-entrypoint.…"   7 minutes ago       Up 7 minutes        80/tcp              docker-nginx
 +</code>
 +
 +----
 +
 +===== Run a Container in Interactive Mode =====
 +
 +<code bash>
 +docker container run --name docker-nginx -it nginx /bin/bash
 +</code>
 +
 +----
 +
 +===== Run a Container and Publish Container Ports =====
 +
 +<code bash>
 +docker container run --name docker-nginx -d -p 8080:80 nginx
 +</code>
 +
 +where:
 +
 +  * **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:
Line 28: Line 148:
  
 This allows continuous use of the container until the terminal is exited by running the **exit** command. This allows continuous use of the container until the terminal is exited by running the **exit** command.
 +
 +----
  
docker/containers/run_a_docker_container.1589359460.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki