We've created an image based on our Dockerfile for Mongodb. Now let's run a container based on that image.
We've created an image based on our docker file for MongoDB. 0:00 Now let's run a container based on that image. 0:03 As before, we'll use the docker run sub command. 0:07 Normally our terminal would be attached to the main process on the running container. 0:10 That's MongoDB in this case, that let's us control that process directly, 0:15 but it prevents us from running any other commands on our docker host. 0:20 So I'm going to add the -d flag, which stands for detach. 0:24 That way after the container launches we'll be dropped back to the shell, 0:28 on our Docker host. 0:34 Our Docker file is set up to expose ports 27017 and 0:35 to 82017 because those are used by MongoDB. 0:39 So next we'll need to publish those two ports using -p options, 0:42 so they can be accessed from our Docker host. 0:47 We'll publish 27017 as 27017, and 0:50 we'll also publish 28017 as 28017. 0:56 We'll also assign an easily recognizable name to our container so that we don't 1:01 have to use the randomly generated name or the container ID to operate on it. 1:05 We do that with the --name option. 1:10 We'll use the name of mongo, lastly, and most importantly, 1:13 we need to provide the name of the image that our container will be based on. 1:17 We'll use the name of the image we provided to Docker Build in 1:22 the previous video mongodb. 1:26 Again we won't provide a particular tag so that it defaults to latest. 1:28 When we hit Enter, Docker will launch a container based on the MongoDB image, 1:34 print the ID for the new container, and 1:39 then drop us back to the shell on our Docker host. 1:42 We can get a list of running containers with the docker ps sub command, 1:45 ps stands for process status. 1:50 Presumably it's named after the ps unix command which gives you the status of 1:52 programs on your computer. 1:56 But docker ps gives you the status of docker containers. 1:58 In the output we'll see a partial ID for each running container, 2:01 the image it was started from, the command that's running. 2:06 When it was created, it's current running status, 2:10 any published ports, and finally it's name. 2:15 So there's our container, started from the MongoDB image and with a name of mongo. 2:19 We can view logs for our container with the docker log sub command. 2:23 We need to identify which container to show logs for, so 2:31 type the first few characters of the container ID 0fe, 2:34 you don't need to type the whole ID, just the first few characters of it. 2:37 Press Enter and 2:42 you'll see a log of output from the processes running on the container. 2:43 We can see a message that MongoDB is starting up at the top. 2:49 And there's a message that it's listening on port 27017 down at the bottom. 2:52 Since we provided the name for our container when we launched it, 2:58 we can use that name and commands instead of the more cumbersome container ID. 3:02 Let's try the docker logs command again but 3:06 this time we'll use the container name, mongo. 3:09 We'll get the same result. 3:12 The logs say that MongoDB is listening for connections so let's try connecting to it. 3:13 Here in the shell for our Docker host, we'll run the MongoClient, 3:19 which connects to local host port 27017, by default. 3:23 If you don't have the MongoClient installed on your computer, and 3:26 would like to, see the teacher's notes for installation instructions. 3:30 The client won't care at all if Port 27017 is being forwarded to a docker container 3:35 as long as it gets a response from a Mongo database, it'll work just fine. 3:40 We can run the Mongo show DBs command to show a list of databases. 3:44 And run the exit command to disconnect and return to the shell on our Docker host. 3:50 We can execute additional commands on the container while keeping 3:55 the main process running with the docker exec command. 3:59 So if we want to open a shell and look around in the container, we can run docker 4:03 exec, pass the i and t flags to open an interactive terminal session. 4:08 Specify the ID or name of the container we want to execute on, mongo, 4:13 and lastly give the executable we want to run, /bin/bash. 4:17 Running the bash executable launches a shell on the container, 4:21 allowing us to run commands just like we would have on the other Linux system. 4:26 So we can print the working directory, we can get a list of the directory contents, 4:32 and we can show a list of running processes. 4:36 We can type exit, when we want to quit the shell and return to our host system. 4:39 Once we're done working with the Mongol database, 4:45 we can safely shut the container down. 4:48 We do that with docker stop sub command, again we need to specify which container 4:50 we're working with using either the container ID or the container name. 4:56 We'll use the name mongo, oops, made a little typo here at the start, 5:01 with the container stopped, it'll no longer appear in the output of docker ps. 5:06 And of course, it won't be able to accept network connections either. 5:11 If we try running the mongo client again now, it'll fail to connect. 5:15 If we need the container again, we can start it back up with docker start mongo. 5:20 After that it'll appear in the docker ps output again, and 5:27 it'll resume accepting network connections. 5:31 The mongo client will resume work, docker stop mongo will shut it down again. 5:34 Docker ps only shoes running containers. 5:45 We can get a list of all containers by adding the -a flag. 5:45 If we do that we'll see the stopped mongo container listed in the output. 5:52 We can remove these stopped containers with a docker rm sub command. 5:57 Again we need to specify which container, so provide either the container ID or 6:04 the container name, we'll use the name mongo. 6:10 If we run docker ps -a again, the mongo container will be gone from the list. 6:13 Remember how we tried to remove the temp image earlier, and 6:19 couldn't because the container based on it still existed? 6:22 We can see a container based on the temp image, here in the output of docker ps -a. 6:25 Let's remove that container as well using it's ID. 6:30 So I'll copy the ID and enter the command, docker rm and paste the ID here. 6:36 Then if we try to remove the temp image again with docker 6:42 rmi that's remove image temp it should be successful. 6:47 Looks like I'm still getting an error that I can't remove it though. 6:53 Let's try docker ps -a again. 6:58 It looks like we have a couple additional containers that are based on the temp 7:01 image. 7:05 So I'm gonna remove those one by one, I'll copy the ID, docker rm and remove that ID. 7:06 And then copy the ID for the second container and docker rm that as well. 7:12 Okay, let's try this one more time, docker remove image temp, 7:17 and this time, removing the image is successful. 7:23 Now you know the basic commands to manage images and containers, 7:27 but Docker has some great facilities for sharing images too. 7:31 We'll look at those next. 7:34
You need to sign up for Treehouse in order to download course files.Sign up