Beginner’s Guide to Docker (Part 2): Building and Running Your First Image

Jimmy Cheung
3 min readAug 10, 2020

--

Now that you have the basics down for Docker in Part 1, we can move onto actually building and image and running an image.

Dockerfile

First, please note that everything from Docker starts with the Dockerfile. The Dockerfile is a file, often in the root directory of a package or repo, that details how the Docker image should be built. Here is a basic Dockerfile, with basic details on what each line will do:

# Use the official image as a parent image.
FROM node:current-slim

# Set the working directory.
WORKDIR /usr/src/app

# Copy the file from your host to your current location.
COPY package.json .

# Run the command inside your image filesystem.
RUN npm install

# Add metadata to the image to describe which port the container is listening on at runtime.
EXPOSE 8080

# Run the specified command within the container.
CMD [ "npm", "start" ]

# Copy the rest of your app's source code from your host to your image filesystem.
COPY . .

As shown, the Dockerfile will guide docker on how to build the image, and what the image will do when it is run into a container. There are also other commands and flags that can be used, as shown in the Dockerfile reference.

Building an image

git clone https://github.com/dockersamples/node-bulletin-board
cd node-bulletin-board/bulletin-board-app

First, to build an image, you will need an app with a Dockerfile already. Docker has generously provided some sample apps, such as this node-bulletin-board. You can clone it and then cd into the app.

If you check the repo, you can see there is a pretty simple Dockerfile for this app that matches the Dockerfile above.

After downloading the repo, you can build the docker image using the following command:

docker build --tag bulletinboard:1.0 .

After some loading, this will create a Docker image that you can check exists on your system using:

docker images

Running the image

Like in the initial article, you can now run the app that has been shown:

docker run --publish 8000:8080 --detach --name bb bulletinboard:1.0 

‘docker run’ has a lot of different flags that you can reference here.

In the above command there are three distinct flags:

  • — publish: Docker will publish the container to become accessible by your home network. Remember, you are creating a siloed container that is generally not accessible unless these flags are enabled! So, in this case, your local machines port 8000 is now being forwarded to port 8080 on the Docker. Hence, after running this command, you can now access the app at localhost:8000.
  • — detach: By default, without this flag, running the Docker command will require you to leave the terminal window open. This can be convenient, as closing the terminal window can close down your container which you could have been testing with. With — detach, the app will run in the background.
  • — name: This is a simple one — the name of the docker container is now ‘bb’. By default, Docker will randomize the name of your container if you do not provide one.

Accessing the app and cleaning up

As mentioned before, now the app is accessible in localhost:8000. You can now visit it and play around with the bulletin board app.

After you are done with this, you can then kill the container using the following command:

docker rm --force bb

You can also remove the image using this command:

docker rmi bulletinboard:1.0

Conclusion

With this, we have introduced another wrinkle into the Docker experience. We can now build images that you or other people can run. Immediately, you can see the flexibility and versatility of being able to dictate exactly what dependencies or versions of images your containers/development apps are using. And all of this was as simple as spinning up a text file using

Sources

Docker’s guide to building and running the image was referenced and used heavily for this article.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response