This is an old revision of the document!
Table of Contents
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.
If the image doesn't exist on your local machine, Docker will attempt to fetch it from the public image registry.
For example, if you ran /bin/echo hello world as your command, the container will start, print hello world and then stop:
docker run ubuntu /bin/echo hello world
NOTE: Docker containers stop running as soon as the command they issued is complete, so this example will stop the container when the echo command has displayed “hello world”.
An alternative method is:
docker run -ti ubuntu
- -t: Runs a terminal.
- -i: Allow us to interact with it.
This allows continuous use of the container until the terminal is exited by running the exit command.
Run a Container Under a Specific Name
docker container run --name docker-nginx nginx
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
docker container run --name docker-nginx -d nginx
Verify the running container:
docker ps
returns:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0bb07d011cc9 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp docker-nginx