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
FROMline specifies that this image will be based on another image, named
mongo, that has MongoDB already installed.
CMDline specifies a command that will be run when the container starts. In this case, it's the MongoDB service.
- And the
EXPOSElines 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.
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
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
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:
docker run -p 27017:27017 -p 28017:28017 mongotest
- 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.
You'd expect any software that could serve the needs of Facebook and 0:00 Google would be hard to use, right? 0:03 Using Docker is actually easy. 0:05 Here's a Docker configuration file that builds a Mongo DB service. 0:07 See the teacher's notes of you'd like to download a copy yourself. 0:11 This file should be named Docker file with the capital D and 0:15 no extension and saved in a directory on your host machine. 0:17 The docker file is used to set up an image, a self-contained package, that 0:21 includes an operating system and all the other dependencies your app needs to run. 0:25 Don't worry about all the details of this file right now, but 0:29 let me give you a quick overview of what it does. 0:32 The FROM line specifies that this image will be based on another image named Mongo 0:34 that has Mongo DB already installed. 0:39 The command line specifies a command that will be run when the container starts. 0:41 In this case, it's the MongoDB service. 0:45 And the exposed lines is expose network ports within the container to the host 0:48 operating system. 0:52 This lets other apps make network connections to the apps 0:54 running inside the container. 0:56 With this docker file saved to a directory, we can use the docker command 0:58 from our terminal to build an image that our mongo containers will be based on. 1:02 We just run the docker command with the build subcommand and 1:06 then pass the -t flag to tag the image. 1:10 We'll use an image name of mongo test. 1:14 Finally we'll have it running the current directory which contains our docker file. 1:18 When we run this command, Docker will go through all the instructions in the Docker 1:22 file and carry them out. 1:26 A process it may take a little while. 1:27 It downloads the Mongo image to use as a base 6, 1:30 sets mongodb up to run by default and exposes our requested ports. 1:33 None of these changes are made to the host operating system by the way. 1:38 It's all happening inside the image. 1:42 Now let's create a new container based on the image so we can try it out. 1:45 We run the docker command again. 1:49 This time with a sub command of run. 1:53 In order to be able to communicate with Mongo DB, 1:55 we need to add a couple things to the command line. 1:58 Even though we exposed ports 27017 and 28017 in the Docker image, 2:01 those ports won't be accessible unless we also publish them. 2:07 That is, make them accessible via a port on the host OS. 2:10 So let's publish the exposed port 27017 first. 2:14 We need to pass the -p flag, which stands for publish, to docker run. 2:18 Then we need to type the port on the host that we want to publish the exposed 2:24 port as. 2:28 We could publish it on a different port number than we exposed, but 2:29 we'll just keep it the same and publish it at 27017. 2:33 Then we type a colon, and 2:36 the number of the exposed port on the container which is also 27017. 2:38 Then we'll do the same for port 28017, 2:43 publish 28017 as 28017. 2:48 Lastly, we provide the name of the image we want to base our container on which is 2:51 mongotest. 2:56 The container will be created. 3:00 And as we specified in our docker file, 3:02 the mongod command will be run within the container. 3:04 And because we published the two exposed ports, we can switch to another terminal 3:07 window and connect to those ports with the mongo client. 3:11 We can then interact with the service running on the container just as we would 3:15 anywhere else. 3:19 So let's run the show dbs command. 3:20 And it shows our available databases. 3:23 And let's choose exit to exit out of the client. 3:25 But the Mongo service running in your container isn't just accessible from your 3:29 local machine. 3:33 It can be reached over the network by any existing service. 3:34 If you want, it could be deployed into any production environment. 3:38 Now imagine applying the same process to all the other software 3:41 your organization runs. 3:44 No longer would your team to have to pass around brittle scripts and build files or 3:47 match many different dependencies together. 3:51 With Docker you can isolate your various services and deploy them with ease. 3:54 And when you need to scale up the number of instances or distribute 3:58 them over the network differently, Docker will make that easy too. 4:01
You need to sign up for Treehouse in order to download course files.Sign up