docker:containers:run_a_docker_container
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
docker:containers:run_a_docker_container [2020/11/11 11:38] – 192.168.1.1 | docker:containers:run_a_docker_container [2020/11/11 12:12] (current) – 192.168.1.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Docker - Containers - Run a Docker Container ====== | ====== Docker - Containers - Run a Docker Container ====== | ||
- | |||
- | By default, Docker runs a container in **attached** mode. That means it’s attached to the terminal session and output of the process will be displayed on your terminal. | ||
- | |||
- | Containers can also be run in detached mode, which runs the process in the background, and keeps the container running after exiting from the terminal session. | ||
- | |||
- | ---- | ||
- | |||
Launching a container is simple as **docker run** + the image name you would like to run + the command to run within the container. | Launching a container is simple as **docker run** + the image name you would like to run + the command to run within the container. | ||
Line 12: | Line 5: | ||
If the image doesn' | If the image doesn' | ||
- | For example, if you ran **/bin/echo hello world** as your command, the container will start, print hello world and then stop: | + | Containers can be run in different modes: |
- | + | ||
- | <code bash> | + | |
- | docker | + | |
- | </ | + | |
- | + | ||
- | <WRAP info> | + | |
- | **NOTE**: | + | |
- | </ | + | |
+ | * **Attached mode**: | ||
+ | * **Detached mode**: | ||
+ | * **Interactive mode**: | ||
---- | ---- | ||
- | An alternative method is: | + | ===== Run a Container in Attached Mode ===== |
<code bash> | <code bash> | ||
- | docker run -ti ubuntu | + | docker |
</ | </ | ||
- | | + | <WRAP info> |
- | * **-i**: Allow us to interact with it. | + | **NOTE:** |
+ | </ | ||
- | This allows continuous use of the container until the terminal is exited by running the **exit** command. | ||
---- | ---- | ||
Line 71: | Line 59: | ||
0bb07d011cc9 | 0bb07d011cc9 | ||
</ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Run a Container in Interactive Mode ===== | ||
+ | |||
+ | <code bash> | ||
+ | docker container run --name docker-nginx -it nginx /bin/bash | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Run a Container and Publish Container Ports ===== | ||
+ | |||
+ | <code bash> | ||
+ | docker container run --name docker-nginx -d -p 8080:80 nginx | ||
+ | </ | ||
+ | |||
+ | where: | ||
+ | |||
+ | * **8080** is the port of the Docker host. | ||
+ | * **80** is the port of the container where the application is running. | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | An optional host-ip can also be set: | ||
+ | |||
+ | <code bash> | ||
+ | docker container run -p [host-ip]: | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Remove the Container After Exit ===== | ||
+ | |||
+ | <code bash> | ||
+ | docker container run --rm mongo | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== 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 / | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | This option is useful for anyone running a database or application that requires persistence within Docker. | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Example ===== | ||
+ | |||
+ | For example, if you ran **/bin/echo hello world** as your command, the container will start, print hello world and then stop: | ||
+ | |||
+ | <code bash> | ||
+ | docker run ubuntu /bin/echo hello world | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE**: | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | An alternative method is: | ||
+ | |||
+ | <code bash> | ||
+ | docker run -ti ubuntu | ||
+ | </ | ||
+ | |||
+ | * **-t**: | ||
+ | * **-i**: | ||
+ | |||
+ | This allows continuous use of the container until the terminal is exited by running the **exit** command. | ||
+ | |||
+ | ---- | ||
docker/containers/run_a_docker_container.1605094693.txt.gz · Last modified: 2020/11/11 11:38 by 192.168.1.1