未加星标

Docker on Windows - Part 3 Creating A Container with Docker Compose

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Welcome back and thank you for checking out my third and final blog post in the series of running Docker on windows. In the first blog post, we set up Docker on Windows. On the second, we spun up an Nginx container. Today, we're going to spin up an Nginx container, but do it the automated way with Docker Compose! Docker Compose is a manifest file of your container and it's settings. You can have one container in your Docker Compose file or multiple. Let's get started.

First, let's confirm Docker Compose is installed and running on your system. Open up PowerShell and run:

docker-compose

If you see several switches and commands, you are good to go! If not, please confirm you installed Docker with the instructions in the first blog post of this series.


Docker on Windows - Part 3 Creating A Container with Docker Compose

Next, we're going to open up VSCode. If you do not have VSCode installed, please do so by following this link: https://code.visualstudio.com/

Once VSCode is open, we want to ensure we have a few extensions. Those two extension are YAML by RedHat and Docker.


Docker on Windows - Part 3 Creating A Container with Docker Compose
Docker on Windows - Part 3 Creating A Container with Docker Compose

Next, let's go ahead and open up a new file. We can save this file on our desktop.

Please Note: For production code, you'll want to get in the habit of storing this in a Git repo. That way you can write whatever Compose files you want, then git push the code up to your repo where it is safely stored. For testing purposes, we'll go ahead and put it right on our desktops.


Docker on Windows - Part 3 Creating A Container with Docker Compose

Now, let's start writing our Compose file!

The first thing we'll specify is our API version for Docker Compose. This will be version 3.


Docker on Windows - Part 3 Creating A Container with Docker Compose

Next, we're going to start with our service block. The service block in our case, is Nginx.


Docker on Windows - Part 3 Creating A Container with Docker Compose

Please note the two spaces under "services" for "nginx". This is a standard YAML syntax. Please ensure to follow it so you don't receive any errors.

Let's call our image now. Remember in part 2 of the docker series when we pulled down an image? This is the same thing essentially.


Docker on Windows - Part 3 Creating A Container with Docker Compose

Now that we have our image, let's give our container a name!


Docker on Windows - Part 3 Creating A Container with Docker Compose

Next we're going to do a little Docker trick. One of the features of a container is to start the container > run the application > stop the container. In our case, we don't want to do that. We're going to run a command to tail anything. This essentially keeps the container running because it's a never-ending command.


Docker on Windows - Part 3 Creating A Container with Docker Compose

Let's specify our volumes. Volumes can be used for a few purposes. Specifying persistent storage and mounting a specific file in the container. For our purposes, we want to mount a specific nginx.conf config file in our container. We will do it like so:


Docker on Windows - Part 3 Creating A Container with Docker Compose

The first part is where my Nginx config that I want to import into my container is living. In my case, it's on my desktop. Then I have a colon separating where the config is, and where it's going. In our case it's going to /etc/nginx/nginx.conf. Look at it as the "source" is on the left and the "destination" is on the right. Putting in an Nginx config file is purely your choice. I did it for the purposes of showing a part of what volumes are.

Finally, we're going to go ahead and specify our ports.


Docker on Windows - Part 3 Creating A Container with Docker Compose

It is very important to have that colon followed by the port again. If you don't, Dockers networking will forward your app to a different port.

Next, let's go ahead and save that to our desktop. We want to name it "docker-compose.yml" as that is the naming convention for a Docker Compose file.

Let's get back into PowerShell and kick off our Docker compose config. You'll want to run:

docker-compose up -d

What this will do is spin up the Docker Compose config file that's in your location of where you are in your shell, and the -d is to detach from the container. If you don't do that, your shell will be stuck in the "running" state of the container and if you CTRL + C, it'll kill the container.


Docker on Windows - Part 3 Creating A Container with Docker Compose

Note that my current location in my shell is my desktop, which is where my Docker Compose file is living.

Now, let's kick it off!


Docker on Windows - Part 3 Creating A Container with Docker Compose

My screenshot shows "creating". This was because I was testing this prior :).

Because Docker needs access to your C: drive, you may see a pop-up on the bottom right of your screen to allow access to Docker. You'll then be prompted for your password. This is, in short, a UAC prompt, but for Docker. If you are not comfortable doing that, please feel free to run this in a VM instead.


Docker on Windows - Part 3 Creating A Container with Docker Compose

Now, let's run docker container ls to see our container running.


Docker on Windows - Part 3 Creating A Container with Docker Compose

Finally, let's do a docker exec -ti yourcontainerid /bin/bash and cat /etc/nginx/nginx.conf

If you imported an nginx.conf file, you'll see your entries. In my case, I just created a bogus nginx.conf config for testing purposes.


Docker on Windows - Part 3 Creating A Container with Docker Compose

There you have it! You have now successfully spun up a container in an automated fashion with Docker Compose.

I truly hope you all have enjoyed the 3 part Docker in Windows series. It was a pleasure writing the content and thank you everyone for reading.

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

代码区博客精选文章
分页:12
转载请注明
本文标题:Docker on Windows - Part 3 Creating A Container with Docker Compose
本站链接:https://www.codesec.net/view/621139.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(88)