Bird
Raised Fist0
Dockerdevops~5 mins

Running a container with docker run - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you want to quickly start a program inside a container without setting up complex files. The docker run command lets you do this by creating and starting a container from an image in one step.
When you want to test a new application image quickly without writing configuration files
When you need to run a simple web server on your local machine for development
When you want to run a database container temporarily for testing
When you want to try out a command-line tool inside a container without installing it on your computer
When you want to expose a container's service port to your local machine to access it via a browser
Commands
This command starts a new container named 'my-nginx' from the nginx image version 1.23.3. It maps port 80 inside the container to port 8080 on your computer, so you can access the web server at localhost:8080.
Terminal
docker run --name my-nginx -p 8080:80 nginx:1.23.3
Expected OutputExpected
Unable to find image 'nginx:1.23.3' locally 1.23.3: Pulling from library/nginx Digest: sha256:... Status: Downloaded newer image for nginx:1.23.3 2024/06/01 12:00:00 [notice] 1#1: using the "epoll" event method 2024/06/01 12:00:00 [notice] 1#1: nginx/1.23.3 2024/06/01 12:00:00 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 2024/06/01 12:00:00 [notice] 1#1: OS: Linux 5.10.0-21-amd64 2024/06/01 12:00:00 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2024/06/01 12:00:00 [notice] 1#1: start worker processes 2024/06/01 12:00:00 [notice] 1#1: start worker process 31
→
--name - Assigns a custom name to the container for easier management
→
-p - Maps a port from the container to the host machine
This command lists all running containers so you can verify that 'my-nginx' is running and see its details.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES abcdef123456 nginx:1.23.3 "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp my-nginx
This command fetches the default web page served by the nginx container through the mapped port to confirm the server is working.
Terminal
curl http://localhost:8080
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> </head> <body> <h1>Welcome to nginx!</h1> </body> </html>
This command stops the running 'my-nginx' container when you no longer need it.
Terminal
docker stop my-nginx
Expected OutputExpected
my-nginx
This command removes the stopped container to free up system resources.
Terminal
docker rm my-nginx
Expected OutputExpected
my-nginx
Key Concept

If you remember nothing else from this pattern, remember: docker run creates and starts a container from an image in one step, optionally mapping ports and naming the container for easy access.

Common Mistakes
Not mapping container ports to host ports with -p flag
Without port mapping, services inside the container cannot be accessed from your computer
Always use -p host_port:container_port to expose container services
Using docker run without --name and then losing track of container ID
It becomes hard to manage or stop the container without a memorable name
Use --name to assign a clear name to your container
Trying to run a container from an image that does not exist locally without internet access
Docker cannot download the image and the command fails
Ensure you have internet or pre-pull the image with docker pull before running
Summary
Use docker run to start a container from an image with one command.
Map container ports to your computer ports with -p to access services.
Name your container with --name to manage it easily later.

Practice

(1/5)
1. What does the docker run command do?
easy
A. Starts a new container from a specified image
B. Builds a new Docker image from a Dockerfile
C. Stops a running Docker container
D. Deletes an existing Docker image

Solution

  1. Step 1: Understand the purpose of docker run

    The docker run command is used to start a new container based on a Docker image.
  2. Step 2: Differentiate from other Docker commands

    Commands like docker build create images, docker stop stops containers, and docker rmi removes images, so they are not correct here.
  3. Final Answer:

    Starts a new container from a specified image -> Option A
  4. Quick Check:

    docker run = start container [OK]
Hint: Remember: run = start container from image [OK]
Common Mistakes:
  • Confusing 'run' with 'build' command
  • Thinking 'run' deletes images
  • Mixing 'run' with stopping containers
2. Which of the following is the correct syntax to run an interactive container with a bash shell using the image ubuntu?
easy
A. docker run ubuntu -it bash
B. docker run -d ubuntu bash
C. docker run -it ubuntu bash
D. docker run ubuntu -d -it bash

Solution

  1. Step 1: Identify flags for interactive mode

    The -it flag is used to run a container interactively with a terminal attached.
  2. Step 2: Check command order and flags

    The correct syntax places flags immediately after docker run, then the image name, then the command to run inside the container.
  3. Final Answer:

    docker run -it ubuntu bash -> Option C
  4. Quick Check:

    Flags before image, -it for interactive [OK]
Hint: Put flags right after 'docker run', then image, then command [OK]
Common Mistakes:
  • Placing flags after image name
  • Using -d for interactive shell
  • Mixing order of command and flags
3. What will be the output of the following command?
docker run --rm alpine echo Hello World
medium
A. Hello World
B. Container ID printed
C. Error: alpine image not found
D. No output, container runs in background

Solution

  1. Step 1: Understand the command components

    docker run --rm alpine echo Hello World runs the echo Hello World command inside an Alpine container and removes the container after it finishes.
  2. Step 2: Predict the output

    The command prints Hello World to the terminal, then the container stops and is removed automatically.
  3. Final Answer:

    Hello World -> Option A
  4. Quick Check:

    Run with echo prints text, --rm cleans up [OK]
Hint: Echo prints text, --rm removes container after run [OK]
Common Mistakes:
  • Expecting container ID output
  • Thinking container stays running
  • Assuming image is missing
4. You run docker run -p 8080:80 nginx but cannot access the web server on port 8080. What is a likely cause?
medium
A. Port mapping syntax is incorrect
B. The container is not running
C. You forgot to add -d to run detached
D. Port 80 inside container is not exposed

Solution

  1. Step 1: Check container running status

    If the container is not running, port mapping won't work and you can't access the server.
  2. Step 2: Verify port exposure and syntax

    By default, nginx listens on port 80 and the syntax -p 8080:80 is correct. -d is optional but recommended for background.
  3. Final Answer:

    The container is not running -> Option B
  4. Quick Check:

    Container must run for port mapping to work [OK]
Hint: Check if container is running before checking ports [OK]
Common Mistakes:
  • Assuming port 80 is not exposed by nginx
  • Thinking -d is required for port mapping
  • Misunderstanding port mapping syntax
5. You want to run a container from the image myapp that needs environment variables API_KEY and DEBUG set, and expose port 5000 on the host to port 80 in the container. Which command is correct?
hard
A. docker run -e API_KEY=123 DEBUG=true -p 5000:80 myapp
B. docker run -p 80:5000 -e API_KEY=123 -e DEBUG=true myapp
C. docker run myapp -e API_KEY=123 -e DEBUG=true -p 5000:80
D. docker run -e API_KEY=123 -e DEBUG=true -p 5000:80 myapp

Solution

  1. Step 1: Set environment variables correctly

    Use -e VAR=value for each environment variable before the image name.
  2. Step 2: Map ports correctly

    Host port 5000 maps to container port 80 with -p 5000:80. The order is flags, then image name.
  3. Step 3: Verify command order and syntax

    Options must come before the image name. docker run -p 80:5000 -e API_KEY=123 -e DEBUG=true myapp reverses ports, C places options after image, D misses -e for second variable.
  4. Final Answer:

    docker run -e API_KEY=123 -e DEBUG=true -p 5000:80 myapp -> Option D
  5. Quick Check:

    Flags before image, correct port and env syntax [OK]
Hint: Put all flags before image; use -e for each env var [OK]
Common Mistakes:
  • Placing options after image name
  • Reversing port mapping order
  • Missing -e for environment variables
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard