Docker Compose vs Dockerfile - Dockerfile Explained - Docker Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi. Welcome to CodingDroplets. In this video we are  going to see about two important things of docker, docker file and docker compose. We'll be  using both docker file and docker compose while developing the projects in our  upcoming videos. So let's get started. First let's see about docker file. What is a  docker file? Docker can build images automatically by reading the instructions from a docker file. A  dockerfile is a text document that contains all the commands a user could call on the command line to  assemble an image. So we can create a docker file and use docker build command to create an  image from the docker file. Then we can use docker run command to create and run a docker  container from the docker image. Now let's have a look on some of the docker files. You can view  the docker files by clicking on the tags provided in the docker image documentation in docker  hub. Let me open the official image of Redis. In the documentation you can see, there are  several tags available. Let me click on this version with alpine operating system. Here you  can see the docker file for that tag in github. There are several instructions mentioned in the  dockerfile like FROM, RUN, ENV etc. The instructions are not case sensitive, however the convention is  for them to be uppercase to distinguish them from the arguments more easily. Docker treats  lines that begin with hash as a comment. Unless the line is a valid parser directive. We'll be discussing about parser directives in a later session. Docker runs instructions in a  docker file in order. A docker file must begin with a from instruction. On the top it is mentioned  from alpine 3.15. So alpine:3.15 is a different image. Means this Redis docker image  is using the docker image of alpine and running some commands in it to download and install redis  and run it. Now let's have a look on the alpine's docker image. So let me search for alpine here. Here you can see the official image of alpine. Let me click on that. So in this docker image  documentation you can see some tags here 3.15, 3.14, 3.13, several versions. So let me click  on the latest version to see the dockerfile. Here you can see the FROM instruction in  the dockerfile and the argument is SCRATCH. So let's find out what is SCRATCH. I'm  searching for scratch docker image and here you can see the docker  image scratch, the official image. I'm clicking on that. So here you can see that  the scratch image is the smallest possible image for docker. It is an empty image. You can  also see the details in the image documentation. So here there is a small example showing  how to use the scratch image. Next let's see some commonly used instructions in  the docker files. First one, FROM. This command allows you to create a base image such as  an operating system, a programming language etc the instructions executed after this  command take place on this base image. Second one, RUN. Used to run specified commands. You can use  several run instructions to run different commands. Third one, CMD. Specifies the instruction that is  to be executed when a docker container starts. Fourth one, ENTRYPOINT. Used to  set executables that will always run when the container is initiated. Unlike CMD  command, Entry Point commands cannot be ignored or overwridden. Fifth one, WORKDIR. To specify  your working directory inside the container. Sixth one, COPY. To copy files or folder from  your local machine to the docker container. Seventh one, ADD. Similar to copy instruction you  can use add instruction to copy files or folders from your local machine to the docker container. However add instruction also allows you to copy files from a URL or a TAR file. EXPOSE. The  expose instruction inside the docker file informs that the container is listening to the  specified port in the network. The default protocol is TCP. LABEL. To add description or metadata for a  docker image. It's a key value pair. The tenth one is VOLUME. Used to create or mount a volume to the  docker container from the docker host file system. Next is USER. Used to set the user name, group  name, uid or gid for running subsequent commands. If not provided the root user will be used. Next  one ENV. Used to set environment variables with key and value. Next one ARG. Also used to set  environment variables with key and value. But this variable will set only during the image build. Not  on the container. And next one is ONBUILD. Used to specify the commands that runs when the new docker  image is used as a base image for another image. Now let's create a docker image from a dockerfile. I have already created a docker file which is having three different instructions. It is having  a FROM instruction to use alpine as the base image. Then I have provided a COPY  instruction to copy the folder named sample files. Let's see what we  have in the sample files folder. Here you can see there are three text files and  I need to copy these three files to the container. Finally I have given a command to sleep for two  minutes for just keeping the container running for two minutes. Otherwise the container will  get stopped immediately. So we are not running any other services. If there are no services  running, then the container will get stopped. So I have opened powershell to execute the docker  build command. As I told in the previous video, you can use powershell or command prompt in windows.  If you are using mac or linux, you can use the terminal. So let me provide docker build command. Then '-t' which is a parameter for assigning a name for the image. So i'm providing 'sample-img'  as the image name. we can also provide the tag along with the image name by separating with a  colon. you can specify a version number if needed. if not provided anything, docker will automatically  assign a latest tag for the image. After that we can provide '-f' parameter which is used to  mention the docker file name. But we don't really need to provide this parameter as we have used the  default docker file name. That is just Dockerfile. So i am just removing the '-f' parameter. So if you are using a different file name, you can provide '-f'. Then the file name here. So now currently we are using the default docker file name. We are not providing this parameter. Then finally, just provide a dot. Then hit enter. Now the alpine latest OS is getting downloaded and finally the image got created. Now I can  see the image using docker images command and you can see here the image name is sample  img. The tag is latest. So as we have not provided a custom tag automatically docker used latest  tag. Then the image id which is a unique id for the image in our machine. Also it is showing  the image size. I can open docker desktop and choose the images option to see the image details.  Here also you can see the image name, tag, image id, then size, all those details. Now let's provide  the docker run command to create container from this image and it will run the container. So we have to provide the image id as well and now if I open docker desktop, I can see that  the container got created and it is running. So from the docker desktop, we can  open the CLI of this container. So otherwise from the terminal, you can just provide  docker exec -it, then the image id slash bin slash sh. That will open the same terminal or the  same command line for the docker. Now here I'm just providing ls command and you can see here there is  a folder named my files. I'm just providing cd my files to go inside the folder and providing the  ls command. And here you can see we have all the three text files which we had in our sample files  folder. So the files got copied to the container. So next let's move to docker compose. What is  docker compose? Docker Compose is a tool to help define and share multi-container applications. It's  an important tool for any application that needs multiple micro services as it allows each service  to easily be in a separately managed container. How to use docker compose? We can use a YAML file  to configure your application's services. The configuration for a docker compose file is done  in 'docker-compose.yml'. You don't need to place this file at the root of your project  like a docker file. Infact it can go anywhere as it doesn't depend on any other code. However if  you are building the images locally, it will need to go in a project folder with the code being  built. Next we are going to create multi-service containers using docker compose. So in my C folder, I have created a docker compose file, 'docker-compose.yml'. This is the content of the docker  compose file. In the first line you can see the version of the docker compose. So I am using docker  compose version 3.7. So in the first line of our docker compose file we can mention the version  of the docker compose which we are going to use. Next I have mentioned volume, 'wp-data'. After that networks, 'wp-network'. After that you can see there are several services. The first service is db. So I have provided a custom name for the container 'mysql-db'. and the image which I am going to use is mysql colon 5.7. So 5.7 is the version which  I am going to use. Then I have mentioned volume for the mysql container. After that  I have mentioned the environment variables. It contains mysql root password, mysql  database name, mysql user, mysql password. Then the port number for mysql. So normally it uses  3306. But I have provided a custom port number triple eight nine and finally I have assigned wp-network for this container. Next service is phpmyadmin I have provided 'php-myadmin' as  the container name and the base image phpmyadmin phpmyadmin and here you can see it is  depending on the db container. After that I have provided the environment variables for  this particular container. Then the port number. I have assigned 8001. After that I have provided  the same network name. Then the final service. That is wordpress. Container name is wordpress-site. Image is wordpress colon latest. So it will pull the latest version of wordpress and  this container also depends on the db container. Andd provided a custom port number for wordpress, 8002. Then the environment variables for the container and volume and network. Now let's  execute the command for creating the containers for this particular services. So I'm  providing docker-compose space up then '-d' to detach the containers  and that's enough. Now docker compose will pull the images from the repository and create the  containers. So initially it is pulling the mysql image and pulling that image got  completed. Now it is going to pull phpmyadmin and that also completed. Now the  next image is for wordpress, the final image. So all the images got downloaded  successfully and the containers are created. Now the containers are running. So I can use  docker images command to see all the images and you can see we have wordpress, mysql  and phpmyadmin. Now let me provide docker ps command to see the running containers and you  can see all the three containers are running. Now I have opened docker desktop and you can see  here demo wordpress this is the application and if I click on this expand button, it will  show the containers running inside it. So mysql, wordpress and phpmyadmin all are running  fine and here you can also see the port numbers each container is listening to. The first db  container is listening to triple eight nine. Wordpress listening to eight zero zero two.  phpmyadmin listening to eight zero zero one. So let's try to open the URLs. 'localhost:8001' and that is the phpmyadmin and you can  see that phpmyadmin is working fine. Now let me try to open 'localhost:8002' and you can  see now wordpress is also working fine. So here we have got the initial setup page for wordpress. So that's it for this video. Hope you liked it. Please subscribe, like and share this video. See you all in the next video. Thank You!
Info
Channel: Coding Droplets
Views: 15,295
Rating: undefined out of 5
Keywords: docker tutorial for beginners, docker tutorial, docker compose, docker container, docker compose vs dockerfile, dockerfile explained, dockerfile basics, dockerfile vs docker compose, dockerfile best practices, dockerfile commands, dockerfile step by step, what is docker compose used for, what is dockerfile, how to write dockerfile, how to use docker compose yml, how to run docker compose file, dockerfile tutorial by example, docker, dockerfile build container
Id: Z44UJUXsOGA
Channel Id: undefined
Length: 15min 58sec (958 seconds)
Published: Mon Apr 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.