字幕列表 影片播放 列印英文字幕 Today we're going to talk about Docker. For a couple different reasons, one of them being that Docker and containerization, in general, are becoming more and more prevalent in the architectures that people are working in in AWS or if you ship something to Digital Ocean. They are just very important to be able to do this, but it's also incredibly important in our development environments, and as I go through tutorials in the future I want to be able to use Docker images in order to ship code as a starting point for some of the applications and projects that we're going to look into. I'm going to take this standpoint working from a mac, we're going to go from start to finish with install Docker and working with Docker images that already exist, to creating Docker containers, and understanding what it's like for us to create our own images and then share those with the public. We're going to get started using Docker for Mac, which is a new application that can be installed into the /Applications directory, and there is also an equivalent version for Windows. If you're working on Linux, you don't actually have to worry about either of these things because Linux can support it natively. So for this I already have Docker for Mac downloaded. I'm going to move over here and just install this. In order to do this I had to uninstall everything that I had on my system so that we could make this as thorough and realistic as possible. So just drag and drop. Now we're going to run Docker for Mac... there it is. So you do have to allow this to use some network privileges, so you have to be an admin user or a sudoer to be able to do this, but it's going to get you set up pretty fast. So it goes on and does it's own thing here, but this will give us a couple different tools. We'll just watch this. So it is currently running. This will give us Docker the command line utility, it's going to run Docker the server for us and then it's going to create a VM that's actually running the Docker server on, and from there it will also give us Docker Compose, which is an orchestration tool that I'm not going to get into at all today, but I will in future videos just because it's so useful from a development standpoint. So, if I type "docker" it gave us this utility. If I run "docker" and look at the version, this is the newest version that came out, as of the time of recording this, just a few days ago. If we look at the containers that are running, we don't actually have anything running, and that's because we didn't tell it to run anything, but we also don't have any images yet. If we look at Docker images you'll see that we have none here. The first thing that you're going to do when working with Docker is you're going to have to start a base image to work off of. For us we're going to just pretend that we're just working with ubuntu. For that we're going to need to run "docker pull ubuntu" and wait for it to download. Before we move too much further on, we should talk about a few of the terms that are going to be thrown around as we deal with more and more Docker. The first one is "images". Images are prepackaged bundles of changes that you've made to a file system, so image that you start with ubuntu in this case which is just going to be the bare bones operating system, and then if we wanted to build something more we would apply changes to it that that we install something new or make a configuration change and that would become a different image by stacking layers. And then from there we'll get into probably the most important thing that we deal with, and that's "containers", and that's actually when you take the image and you run it, and you make use of that container. So for us we'll do that right now. We can do that by doing "docker run" and we'll pass it a few flags to make it interactive, and then we'll give it the command, so we give it the image and the command name, and this will go and run bash inide of an ubuntu thing. So that we can make sure that we're not running on our mac by typing 'uname' and you can see that we're running on Linux. The first thing that we're going to do here is we're going to make a change and see what happens. I'm going to echo "STuff" into Test.txt and then just make sure it's there. Yep, it's right there, right at the beginning. So we'll exit this, and we'll run "docker ps". There are no containers running. Which I didn't actually show you what a container looks like when it is running, but we'll go back into ubuntu and see if we can take a look at what's going on here. So if we run this again and we run "ls" you'll notice that the Test.txt that we had up here is not here, and that is because every time you create a container or you run and image you create something that is entirely new, it is whatever the starting point was for that image, but everything that happens while it's running is ephemoral, or it doesn't matter, it just kind of keeps on churning. There are ways to get around that by sharing some volumes or connecting some network things together, but that's a little bit more complicated and we'll get into that when we talk about docker-compose. If I split this right now and I run "docker ps" you'll see that we do have a container running. Oh this is really ugly to look at though isn't it? So we'll slide this over for a second. And this gives us our container ID, the image that we used, the command that it's running, and how old it is. It also gives it a name, which if you don't provide a name for it, it comes up with some pretty comical names. So this is a "cranky turing". So if we separate this back out. We know that since we ran this container from an image that it always starts at the same spot, regardless of how many times we run a container using the exact same image. From here we're going to transition into something a little bit different, and that is containers are cool and images are cool, but what happens if we want to make our own image. So, imagine that we want to ship somebody ubuntu and we want it to come pre-installed with some things. So, in my particular case, I want to make sure that "wget" is there so that it's easy to install things, and if we run our ubuntu image and we run "wget" you'll see that "oh it's not there". So that's kind of a bummer. There's a really neat way of doing this in Docker and that is through what's known as a Dockerfile. And then from here the Dockerfile is a way to specify the recipe essentially, the steps that we're going to take in order to take an image that already exists, in this case "ubuntu", and turn it into something that more fits our needs, which in this case is that we need ubuntu with wget and we need it to always be there. So for that I'm going to open up Atom, I'm going to create a Dockerfile. The Dockerfile has a slightly different syntax than a normal programming language because its more just a series of steps, kind of like a script except you don't have the same power of calculation that you would have if you were writing something like a ruby or a bash script. You have to follow a certain structure. So the first thing that you have to do when creating a Dockerfile is that you have to say what it's coming from, this is the starting image, or the layer that you're going to begin with. For us we're going to start with Ubuntu, and then from here we need to run some things. Everything that comes after "RUN" is going to be processed inside, and then like I was kind of saying earlier if you install something new, that creates a new layer. Each of these command will create something, a different layer on top of of this. And our image will actually be the final layer that is created because it was built up of all the layers that existed before it. For us we're going to "apt-get update" and then pass it a few flags to make it a little quiet, and the we will also say "apt-get install wget" and we'll pass this just to make it a little quiet too. From here the other thing that you need to have is what is actually going to happen when you run an image, and that is by default you can actually do "docker run ubuntu" and it will go off an run something. I actually don't remember off hand what ubuntu does when you run it by itself, but you can define a default command so that if it's something that's going to, say you have an image that's going to go and process some data and then close, you can just type "docker run my_data_processor_image" and then it will go off, do it's thing, and then just the process itself will die and the container will go away. So we don't have to do that, but in our case I want to because I don't want to have to type "bash" every single time. I'm going to put this in here to where it's just going to run "bash" whenever we do this. We're going to save it. We're going to bounce back over to our terminal sothat we can build this. Behind the scenes I moved us into a different directory, but what you're going to do here is type "docker build", we're going to name our image, in this case it's called "tagging", but we're going to tag it as "wget" and then we're going to say build with the current context, which just means use this directory and look for the Dockerfile that's here. So it's going to go off and figure everything that it needs and then you'll see that these are the steps that it takes. Our first step was to say from this, this represents the layer that is "ubuntu", and then our next step here is run "apt-get update" and also install wget. It will show you everything that it's installing. And then when it's finished, you'll see that it gives you this. So it created an image here, and then you're going to come down here and this next step, yet another layer that we're going to be adding to this is to say that you're going to run "bash" as your default thing. So now if we run "docker images" you can see that we have multiple images here, that is the successful build of the image that we just made for "wget", and then we have the ubuntu one which gives you this original SHA too. The neat thing here is that you can also see the difference in what we did, us running "apt-get update" and "apt-get install" on wget actually installed an extra 46MB worth of space. Let's run our image and see the fruits of our labor here. So if we "docker run -it" and just type "wget", remember that we told it the command it should run is bash. So it should open us right into bash. And we're going to "wget google.com" and it should just work. There you go, so we went and we downloaded the index.html from google.com with the tool that we installed and now everytime we want to work with something that needs "wget" we can run our own image instead of working with the ubuntu image that didn't have that package installed to begin with. So the last thing that I said we were going to do in this short tutorial is that we're going to take our image and we're going to put it on the web so that other people can access it, but I'm also going to show you how to find other images that you can work with. So if we go back to the web browser, if you go to hub.docker.com you can see all of the other images that exist out here. This is looking at my profile page, but if we go to the dashboard here, well "explore", you'll see that there are a bunch of different "official repositories", so Nginx is one for instance that if you just type "docker pull nginx" it will pull this exact image. Normally, you have to work off of things by username and then image name, but ubuntu for instance, this is the exact image that we were using for our purposes before we installed wget on top of it. There are many of these things, so there are a lot of different things that you need necessarily, or that you might want to work with, like say you want to play around with node, but you don't want to install it on your local machine, you can pull the official node image and then you can go into the container that it starts and run bash in there for instance to give youself a shell so that you can work with node directly. Wordpress for instance, you just spin up a container that is already running wordpress and go from there. If we look back at the dashboard, I have a couple just random projects that I've sort of worked with, but this one, the "elixir-ide" is me experimenting and putting an ubuntu image out there that had everything that I needed for working with elixir, so I put a text editor in there that I wanted to use, and then pre-installed syntax highlighting, and pre-installed elixir for it so that I could just spin this up if I ever want to do any and I didn't have to have elixir installed on my machine. We need to push some things up here, and I'll show you how to do that. So if we "docker push wget", you'll see that we... oh I'm actually inside of the container that's why I'm so confused. "docker push wget", you'll see that authentication required and what this did here is that it was trying to say that you're going to push to "library", this is for the official packages, and then it's saying that you're not allowed to do that. So for me, my username on Docker hub is "keiththomps" so we're actually going to tag "wget" as "keiththomps/wget". Now if we look at "docker images" again you'll see that I have "wget" and that I have "keiththomps/wget" and they're the exact same thing, they point to the exact same SHA, they're just two names for the same thing. So from here we'll try to push this. Ahh "Authentication required" again, so this is because you need to be authenticated with Docker in order to push to Docker hub, but not to pull from Docker hub. So to do this you use "docker login", my username's "keiththomps", and I'm going to paste in my password. Alright, it logged in successfully, so if we go back and we try to push this again we'll see what happens. So right now it's going to go off and push each layer, and each layer has a little bit of weight to it. I'm going to chop out the bit while it's uploading and then we can continue after it's done. Now that it's finished, we can go back to Docker hub and see if it uploaded properly. Docker hub isn't the fastest website on the planet, so you have to bear with it sometimes when it takes awhile. Alright, there it is. So we've successfully gone from having Docker not installed on our machine, to installing Docker through Docker for Mac (although there are other ways to install it), to downloading our first image and building our first container, to creating our first image and building a container with that image, and then uploading that image so that other people can share. I hope you enjoyed this, and if you did I would appreciate any feedback you've got and if you want to subscribe or like the video then that would be great too.
A2 初級 美國腔 學習Docker的基礎知識 (Learn the Basics of Docker) 61 9 MingEn Wu 發佈於 2021 年 01 月 14 日 更多分享 分享 收藏 回報 影片單字