How do I run a container?

In this walkthrough, you'll learn the basic steps of building an image and running your own container. This walkthrough uses a sample Node.js application, but it's not necessary to know Node.js.

Running an image in Docker Desktop

Before you start, get Docker Desktop.

Step 1: Get the sample application

If you have git, you can clone the repository for the sample application. Otherwise, you can download the sample application. Choose one of the following options.


Use the following command in a terminal to clone the sample application repository.

$ git clone https://github.com/docker/welcome-to-docker

Download the source and extract it.


Step 2: View the Dockerfile in your project folder

To run your code in a container, the most fundamental thing you need is a Dockerfile. A Dockerfile describes what goes into a container. This sample already contains a Dockerfile. For your own projects, you'll need to create your own Dockerfile. You can open the Dockerfile in a code or text editor and explore its contents.

Viewing Dockefile contents

Step 3: Build your first image

You always need an image to run a container. In a terminal, run the following commands to build the image. Replace /path/to/welcome-to-docker/ with the path to your welcome-to-docker directory.

Tip

To run Docker commands, you must use a terminal. Based on your operating system, you can open a terminal by doing the following:

For Windows, select the Start Menu, specify cmd, and then select Command Prompt.

For Mac, select the Launchpad icon in the Dock, specify Terminal in the search field, then select Terminal.

$ cd /path/to/welcome-to-docker/
$ docker build -t welcome-to-docker .

In the previous command, the -t flag tags your image with a name, welcome-to-docker in this case. And the . lets Docker know where it can find the Dockerfile.

Building the image may take some time. After your image is built, you can view your image in the Images tab in Docker Desktop.

Step 4: Run your container

To run your image as a container:

  1. In Docker Desktop, go to the Images tab.
  2. Next to your image, select Run.
  3. Expand the Optional settings.
  4. In Host port, specify 8089.
    Specifying host port 8089
  5. Select Run.

Step 5: View the frontend

You can use Docker Desktop to access your running container. Select the link next to your container in Docker Desktop or go to http://localhost:8089open_in_new to view the frontend.

Selecting the container link

Summary

In this walkthrough, you built your own image and ran it as a container. In addition to building and running your own images, you can run images from Docker Hub.

Related information:

Next steps

Continue to the next walkthrough to learn how you can run one of over 100,000 pre-made images from Docker Hub.