Run a Docker image as a container

Learn run a docker image as a container with practical examples, diagrams, and best practices. Covers docker, docker-image development techniques with visual explanations.

How to Run a Docker Image as a Container

Hero image for Run a Docker image as a container

Learn the fundamental steps to launch and manage Docker containers from existing images, covering basic commands and essential concepts.

Docker has revolutionized how applications are developed, shipped, and run. At its core, Docker uses images as blueprints to create isolated environments called containers. Running a Docker image as a container is the most basic and crucial operation for anyone working with Docker. This article will guide you through the process, from understanding the docker run command to managing your running containers.

Understanding Docker Images and Containers

Before diving into commands, it's important to differentiate between a Docker image and a Docker container. A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and configuration files. Think of it as a template or a snapshot of an application and its dependencies.

A Docker container is a runnable instance of an image. When you run an image, Docker creates a container from it. Each container is isolated from other containers and from the host system, ensuring consistency across different environments. You can have multiple containers running from the same image, each operating independently.

graph TD
    A[Docker Image] --> B{docker run command}
    B --> C[Docker Container (Instance 1)]
    B --> D[Docker Container (Instance 2)]
    C -- Isolated --> D
    C -- Isolated --> E[Host System]
    D -- Isolated --> E

Relationship between Docker Images and Containers

The docker run Command: Your Gateway to Containers

The docker run command is the primary way to start a new container from a Docker image. It's a powerful command with many options, allowing you to configure almost every aspect of the container's runtime environment. The basic syntax is straightforward: docker run [OPTIONS] IMAGE [COMMAND] [ARG...].

Basic Container Execution

Let's start with the simplest way to run a container. We'll use the hello-world image, which is a great way to verify your Docker installation and understand the basic run command.

docker run hello-world

Running the 'hello-world' Docker image

When you execute this command, Docker performs the following actions:

  1. Pulls the image: If hello-world isn't on your local machine, Docker downloads it from Docker Hub.
  2. Creates a container: A new container is created from the hello-world image.
  3. Runs the command: The default command embedded in the hello-world image is executed (which prints a message).
  4. Exits the container: Once the command finishes, the container stops and exits.

Running Containers in Detached Mode

For most applications, you'll want your container to run in the background without tying up your terminal. This is achieved using the -d or --detach flag. When a container runs in detached mode, Docker prints the container ID and then returns control to your terminal.

docker run -d nginx

Running an Nginx web server in detached mode

After running this, you can verify that your Nginx container is running using docker ps.

docker ps

Listing currently running containers

Mapping Ports for External Access

Containers are isolated by default. If you're running a web server like Nginx, you'll need to map a port from your host machine to a port inside the container to access it from your browser. This is done with the -p or --publish flag, using the format HOST_PORT:CONTAINER_PORT.

docker run -d -p 8080:80 nginx

Running Nginx and mapping host port 8080 to container port 80

Now, you can open your web browser and navigate to http://localhost:8080 to see the default Nginx welcome page.

Naming Your Containers

Docker automatically assigns a random, often whimsical, name to your containers (e.g., jolly_hoover, pensive_mccarthy). While functional, these names can be hard to remember. You can assign a custom, human-readable name using the --name flag, which makes it easier to manage and refer to your containers.

docker run -d -p 8080:80 --name my-nginx-server nginx

Running Nginx with a custom container name

Now, instead of using the container ID, you can use my-nginx-server in other Docker commands, such as stopping or restarting it.

docker stop my-nginx-server
docker start my-nginx-server

Stopping and starting a named container

Removing Containers

When a container is no longer needed, it's good practice to remove it to free up resources. You can remove a stopped container using docker rm.

docker rm my-nginx-server

Removing a stopped container by name

Putting It All Together: A Practical Example

Let's combine these concepts to run a simple web application container.

1. Pull an image (optional but good practice)

While docker run pulls images automatically, explicitly pulling can sometimes give better feedback on download progress.

2. Run the container in detached mode with port mapping and a custom name

This command will run a wordpress container, making it accessible on your host's port 8000, and name it my-wordpress-app.

3. Verify the container is running

Check the status of your newly launched container.

4. Access the application

Open your web browser and navigate to http://localhost:8000 to see your WordPress installation.

5. Stop and remove the container when done

Clean up your resources after you're finished experimenting.

# Step 1: Pull the WordPress image
docker pull wordpress

# Step 2: Run the container
docker run -d -p 8000:80 --name my-wordpress-app wordpress

# Step 3: Verify it's running
docker ps

# Step 5: Stop and remove (when done)
docker stop my-wordpress-app
docker rm my-wordpress-app

Running a WordPress container step-by-step

This example demonstrates how to effectively use docker run with common options to deploy a functional application. Remember that for a full WordPress setup, you would typically also need a database container (like MySQL), which would involve more advanced networking and volume concepts.