Running a Docker Container
This post lists the top 5 most frequently asked questions about running Docker containers. Docker is one of the most well-documented services out there, but it's always hard to find the exact question you're looking for. If you've ever had a question like the one below, this list should help.
1. The Command to run a Docker Container (Run an Image)
The command to run a Docker container is docker container run
.
This command performs the same function as the docker run
command, and is a new addition to Docker's modernized command structure.
I find docker container run
to be much more intuitive and easy to distinguish, even when used in conjunction with the other commands we'll look at later,
I recommend using this command.
The basic `docker container run' command looks like this
$ docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]]
[OPTIONS]
: Optional parameters used to run the container. For example,-d
runs the container in the background,-p
sets the port mapping between the host and the container, and-v
is used to mount a volume.IMAGE
: This is the Docker image to run the container on and must be specified. This can be a local image or an image in a remote registry such as Docker Hub.COMMAND
: The command to run when the container is started. As an optional parameter, If specified, the specified command will be executed instead of theCMD
directive within the Docker file of the Docker image specified in theIMAGE
part.[ARG]
: The argument passed to[COMMAND]
, this is an optional parameter.
Here's an example
$ docker container run -d -p 8080:80 --name my_container nginx
To explain the options we used here, they are
-d
: Run the container in the background (detach mode).-p 8080:80
: maps port8080
on the host to port80
on the container.--name my_container
: Set the name of the container tomy_container
.nginx
: Run the container based on the latest version of the officialnginx
Docker image. If you don't have this version of thenginx
image on your local environment, it will be downloaded from the Docker Hub.
In other words, the above command creates a container named my_container
based on the nginx
image, and assigns port 8080
on the host to port
to the container's 80
port, and runs the container in the background.
2. Troubleshooting a Non-Running Container
As we saw in Section 1, you may have a frustrating experience when your container fails to run properly despite using the `docker container run' command. There are many reasons why a Docker container might not run, but here are some of the more common ones and their solutions.
2.1. Image doesn't exist
In order to run a Docker container, an image of that container must exist on your local system or be available for download from a remote location such as Docker Hub. If the image does not exist and the download fails, check your network situation and try to manually download the image using the `docker image pull' command and then run the container.
2.2. Docker Service Not Running
If the docker service is not running, you will not be able to run the container.
In this case, run systemctl start docker
on Linux or Docker Desktop on MacOS and Windows, and check the tray icon to see if Docker is working properly.
2.3. Insufficient Resources for Containers
Containers use a lot of CPU or memory, and if your system doesn't have enough resources, they might not run. In this case, you need to terminate unnecessary applications or containers to free up the required resources, and then run the container.
2.4. Container Configuration Errors
If the dockerfile
or docker container run
command contains incorrect settings, the container might not run.
In this case, you must check the settings, fix the problem, and run the container again.
Please read Section 1 again and try to run it.
2.5. Container terminates immediately
If a container's executable command terminates quickly (for example, a command that outputs "Hello, World!) container may exit immediately after execution and appear not to be running. In this case, you should either modify the command to keep the container running, or use the `docker container logs' command to see the container's output.
If you are still having problems after troubleshooting these issues, use the docker container logs [container_id]
command to view the container's logs,
You can add the -it
option to the docker container run
command to run the container in interactive mode and see the problem for yourself.
Please see Section 5 for this part.
3. How to Keep Container Autorun Hooks Running
There are several ways to autostart a Docker container. The most common ways are to use a Docker restart policy or to use Docker Compose.
3.1. Docker Restart Policy
Docker provides a restart policy that automatically restarts a container when it fails or when the Docker daemon (the program that manages all Docker processes in the background) is started. You can use this to automatically run containers.
To specify a restart policy, you set it by adding the --restart
option to the docker container run
command.
Restart policies have intuitive options such as no
, on-failure
, always
, and unless-stopped
.
For example, the following command instructs the container to always restart when stopped:
docker run -d --restart always my-container
3,2. Docker Compose
Docker Compose is a tool that allows you to define and manage multiple containers. You can use these Docker composites to automatically launch containers.
Docker Compose uses a YAML file format to define services. Within this file, you can set a restart policy to automatically start a container. For example, here is an example of a `docker-compose.yml' file:
version: "3"
services:
web:
image: my-web-app
restart: always
...
With this setting, the `my-web-app' service will always start automatically.
You can also use container orchestration tools, such as Docker Swarm and Kubernetes, to automatically start and manage containers. These tools are useful for scheduling, scaling, and recovering containers within a cluster.
4. How to run container by id
The `docker container run' command we saw in Question 1 creates and runs a new container. If you want to run an already created container again, you need to follow the container id run method.
Two commands are required.
First, to get the ID of the stopped container, type the following command
$ docker container ls --all
Copy the part of the container ID you found and run it with the following command
docker container start [container-id].
5. How to run container sh [#5].
To run a shell (sh) script inside a Docker container, you can use the `docker container exec' command. This allows you to start a new process inside the running container.
Here are the commands to run a shell inside a Docker container
docker container exec -it [container_id] /bin/sh
Where [container_id]
is the ID of the running Docker container.
The -it
option starts an interactive terminal session with the container, and /bin/sh
is the command to execute.
If your container does not support /bin/sh
, but only /bin/bash
, you can type something like this
$ docker container exec -it [container_id] /bin/bash
This will start a shell session inside the container, allowing you to enter commands directly to manipulate files, run processes, etc.
6. Final thoughts
In this post, we have learned how to run Docker containers, how to troubleshoot failures, how to automatically run or keep running containers, how to run by container ID, and how to run shells, including sh, inside containers. I hope you find this post helpful in creating and running new containers with Docker.
