Create and run a simple container
In this step, we will learn how to create and run a simple Docker container. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
We will use the docker run
command to create and run a container. The basic syntax is docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
.
Let's run a simple container using the ubuntu
image and the echo
command. The ubuntu
image is a minimal Ubuntu operating system image.
docker run ubuntu echo "Hello from Docker!"
When you run this command for the first time, Docker will first check if the ubuntu
image exists locally. If it doesn't, it will pull the image from Docker Hub (the default registry). Then, it will create a new container from this image and execute the echo "Hello from Docker!"
command inside the container. After the command finishes, the container will stop.
You should see the output Hello from Docker!
in your terminal.
Now, let's try running a container that stays running. We can use the -d
option to run the container in detached mode (in the background) and the tail -f /dev/null
command to keep the container running without consuming significant resources. We will also give the container a name using the --name
option so we can easily refer to it later.
docker run -d --name my-ubuntu-container ubuntu tail -f /dev/null
This command will pull the ubuntu
image (if not already present), create a container named my-ubuntu-container
, and run the tail -f /dev/null
command in detached mode. The command will output the container ID.
To verify that the container is running, you can use the docker ps
command, which lists running containers.
docker ps
You should see a list of running containers, including my-ubuntu-container
.