Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
We'll use a Dockerfile to build a new image, then run a new container based on that image.
Here's a Docker configuration file that builds a MongoDB service. Save it in a new directory, and name it Dockerfile
, with a capital "D" and no file name extension.
# Dockerfile for building a MongoDB service
# Pull base image.
FROM mongo
# Define mountable directories.
VOLUME ["/data/db"]
# Define working directory.
WORKDIR /data
# Define default command.
CMD ["mongod"]
# Expose ports.
# - 27017: process
# - 28017: http
EXPOSE 27017
EXPOSE 28017
- The
FROM
line specifies that this image will be based on another image, namedmongo
, that has MongoDB already installed. - The
CMD
line specifies a command that will be run when the container starts. In this case, it's the MongoDB service. - And the
EXPOSE
lines expose network ports within the container to the host operating system, so that other apps can make network connections to the apps running inside the container.
With this Dockerfile
saved to a directory, we can use the docker
command from our terminal to build an image that our Mongo containers will be based on. We just run the docker
command with the build
subcommand, and then pass the -t
flag to tag the image. We'll use an image name of "mongotest". Finally, we'll have it run in the current directory, which contains our Dockerfile
.
docker build -t mongotest .
Now, let's create a new container based on the image so we can try it out. We run the docker
command again, this time with a subcommand of run
.
In order to be able to communicate with MongoDB, we need to add a couple things to the command line. Even though we exposed ports 27017 and 28017 in the Docker image, those ports won't be accessible unless we also publish them, that is, make them accessible via a port on the host OS. So let's publish the exposed port 27017 first. We need to pass the -p
flag, which stands for "publish", 27107
, a colon, and the number of the exposed port on the container, which is also 27017
. Then we'll do the same for port 28017: -p 28017:28017
. Lastly, we provide the name of the image we want to base our container on: mongotest
.
docker run -p 27017:27017 -p 28017:28017 mongotest
New Terms
- Software Delivery Pipelines -- When an app is setup so that itβs easily sent through the process of build, test, and deployment. Often referred to as CI or CD (Continuous Integration or Continuous Delivery).
- Dockerized App -- An app that has a Dockerfile made for it and can be built into a Docker image and run as a container.
- Container -- You can think of a container for an app as a real-life shipping container for freight. An app container is also like a VM, but far more lightweight and with the same security and operational isolation from system resources.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up