Docker Tutorial for Beginners

Programming with Mosh
30 Mar 202156:03

Summary

TLDRThe Ultimate Docker Course is a comprehensive tutorial designed to teach software developers how to effectively use Docker in their workflow. Starting with basic concepts and advancing to complex scenarios, the course covers everything from Docker's architecture to practical applications. Instructor Ash Hamadani guides learners through building, running, and shipping applications consistently across different environments. The course also includes a simple project to illustrate Docker's basics and the deployment of a full-stack application, emphasizing hands-on learning. With no prior Docker knowledge required, but a foundation in programming and basic Git commands, this course is ideal for those looking to enhance their software engineering skills with Docker proficiency.

Takeaways

  • πŸ“¦ Docker is a platform for building, running, and shipping applications consistently across different environments.
  • πŸ–₯️ Docker eliminates the 'it works on my machine' problem by packaging applications with all their dependencies into containers.
  • πŸš€ Containers are more lightweight and efficient compared to virtual machines because they share the host OS's kernel.
  • πŸ’‘ Docker uses a client-server architecture, with a client that communicates with a Docker engine server using a RESTful API.
  • πŸ“‹ A Dockerfile is a plain text file that contains instructions for Docker to build an image, which includes everything needed to run the application.
  • 🌐 Docker images can be stored and shared on Docker Hub, allowing easy distribution and deployment on any machine running Docker.
  • πŸ”„ Docker enables consistent development workflows by allowing developers to build, run, and ship applications in a standardized way.
  • πŸ“ Practical hands-on learning is emphasized, with recommendations to take notes, repeat commands, and actively engage with the lessons.
  • πŸ” Understanding basic Linux commands and concepts is important because Docker's foundations are built on Linux.
  • βš™οΈ Docker allows applications to run in isolated environments called containers, which can use different versions of software and dependencies without conflict.

Q & A

  • What is the main focus of the Docker course?

    -The main focus of the Docker course is to teach everything about Docker, from basic to advanced concepts, so that by the end of the course, participants can use Docker like a pro as part of their software development workflow.

  • Who is the instructor of the Docker course and what is their background?

    -The instructor of the Docker course is Ash Hamadani, who has taught millions of people how to advance their software engineering skills through his YouTube channel and his online school, codewithmash.com.

  • What prior knowledge is recommended before taking the Docker course?

    -Before taking the Docker course, it is recommended to have at least three months of programming experience and to have built at least one application. Familiarity with concepts like front-end, back-end, API, database, and basic Git commands is also suggested.

  • What practical project will be used in the course to teach Docker?

    -The course will start with a simple project to understand the basics of Docker and then use Docker to run and deploy a full-stack application with a front-end, back-end, and a database.

  • Why is Docker beneficial for application development and deployment?

    -Docker is beneficial because it allows applications to run in consistent environments across different machines, ensuring that if an application works on a development machine, it will work the same way on other machines. It packages applications with all their dependencies, which eliminates issues related to missing files, different software versions, or varying configuration settings.

  • How do Docker containers differ from virtual machines?

    -Docker containers differ from virtual machines in that they are more lightweight, do not require a full operating system, and share the host's OS kernel. Containers start up quickly, use fewer resources, and allow running multiple isolated applications side by side more efficiently than virtual machines.

  • What is the role of the Docker client and server components?

    -The Docker client component interacts with the Docker server component (also called the Docker engine) using a RESTful API. The server component builds and runs Docker containers, which are essentially isolated processes.

  • What is a Docker image and how is it created?

    -A Docker image is a package that contains everything needed to run an application, including a cut-down operating system, runtime environment, application files, libraries, and environment variables. It is created by writing a Dockerfile with instructions and then using Docker to build the image from this Dockerfile.

  • How can Docker images be shared and reused across different machines?

    -Docker images can be shared and reused across different machines by pushing them to a Docker registry like Docker Hub. Once an image is stored on Docker Hub, it can be pulled and run on any machine that has Docker installed.

  • Why is it important to have some knowledge of Linux commands for working with Docker?

    -It is important to have some knowledge of Linux commands for working with Docker because Docker is built on top of basic Linux concepts, and understanding these commands makes it easier to be productive and troubleshoot issues. Most tutorials and documentation are also based on Linux commands.

Outlines

00:00

πŸ“š Introduction to the Ultimate Docker Course

This paragraph introduces the Ultimate Docker Course, which aims to teach students everything about Docker, from basic to advanced concepts. The course is designed for those with at least three months of programming experience and familiarity with software development concepts like front-end, back-end, APIs, and databases. The instructor, Ash HAMADani, is a seasoned educator with a YouTube channel and online school, and he emphasizes the practicality of the course. Students are advised to actively engage with the material by taking notes and practicing the commands discussed in each lesson. The course promises to take learners from zero to hero in Docker, enabling them to use it proficiently in their software development workflows.

05:02

πŸ’» Docker's Benefits and Practical Learning Approach

The paragraph discusses the benefits of Docker, highlighting its ability to create consistent application environments that can run the same on any machine, thus solving common deployment issues. It also touches on Docker's advantages over traditional virtual machines, such as being lightweight and sharing the host's operating system. The instructor suggests a practical learning approach for the course, recommending that students actively practice commands and techniques after each lesson to ensure mastery of Docker. The section concludes with an overview of Docker's architecture and the installation process, setting the stage for hands-on learning in the subsequent lessons.

10:04

πŸ› οΈ Docker Installation and Practical Workflow Overview

This section provides a step-by-step guide on installing Docker, emphasizing the importance of upgrading to the latest version for compatibility with the course's content. It offers specific instructions for Docker Desktop on Mac and Windows, as well as Docker Engine for Linux, including system requirements and the need to enable certain features like Hyper-V on Windows. The paragraph also explains how to verify the installation by checking the Docker version and ensuring the Docker Engine is running. Furthermore, it outlines a typical Docker development workflow, which involves dockerizing an application, creating a Docker image, and running it in a container. The workflow's efficiency is highlighted, demonstrating how Docker simplifies the deployment process and eliminates the need for complex release documents.

15:06

πŸ”§ Dockerfile and Image Creation Process

The paragraph delves into the process of creating a Dockerfile and building a Docker image. It explains the purpose of a Dockerfile, which is to contain instructions for Docker to package an application into an image. The example given starts with a base image, such as a Node image from Docker Hub, and then copies the application files into the image. The paragraph also discusses the use of the 'docker build' command to create an image and 'docker images' to list the available images. The benefits of using a small base image, like Alpine Linux, are highlighted to keep the image size minimal. Finally, the paragraph demonstrates how to run the created image on any machine with Docker and how to publish the image to Docker Hub for sharing and reuse.

20:07

πŸ‹ Exploring Docker Hub and Linux Basics

This section introduces Docker Hub as a registry for Docker images and discusses the importance of understanding basic Linux commands for working with Docker, regardless of the user's operating system. The instructor encourages even Windows users to learn Linux basics due to Docker's foundations in Linux concepts. The paragraph provides a brief overview of different Linux distributions, or 'distros,' and mentions that while they generally support the same set of commands, there can be variations. The focus is on using Ubuntu Linux for the course, and viewers are guided on how to run Ubuntu in a Docker container, emphasizing the ease of access to different Linux environments through Docker.

25:07

πŸ“ Linux File System Navigation and Package Management

The paragraph covers the fundamentals of navigating the Linux file system and using the package manager 'apt'. It explains the hierarchical structure of the Linux file system, starting with the root directory and moving through standard directories like 'bin', 'boot', 'dev', 'etc', 'home', 'root', 'lib', 'var', and 'proc'. The 'pwd' and 'ls' commands are introduced for navigating and listing directory contents, along with options to modify the output. The 'cd' command is used for changing directories, and the paragraph demonstrates how to use auto-completion and shortcuts like 'cd ~' to access the home directory. Additionally, the 'apt' package manager is introduced, with instructions on how to update the package database and install packages like 'nano', a basic text editor for Linux.

30:08

πŸ› οΈ File and Directory Manipulation in Linux

This section focuses on manipulating files and directories in Linux. The 'mkdir' command is used to create new directories, while the 'mv' command serves to move or rename files and directories. The 'touch' command is introduced for creating new files, and the 'ls' command is used to list files with various options for formatting the output. The paragraph also covers renaming files using the 'mv' command and demonstrates how to remove files with the 'rm' command, including the use of the '-r' option for removing directories and their contents. An exercise is suggested for the viewer to practice creating, renaming, and removing files and directories in their home directory.

35:08

✍️ Editing and Viewing Files in Linux

The paragraph introduces the 'nano' text editor and demonstrates how to install it using 'apt', create a new file, and edit it using nano. It also presents alternative commands for viewing file contents, such as 'cat' for small files, 'more' and 'less' for long files, and 'head' and 'tail' for viewing portions of files. The 'cat' command is highlighted for its ability to concatenate files, and the paragraph concludes with a practical exercise for the viewer to get a long listing of files in a directory and redirect the output to a file.

40:09

πŸ” Redirection and Searching in Linux

This section discusses the concept of redirection in Linux, which allows for changing the source of standard input and output. The greater than sign (>) is used to redirect output from the screen to a file, and the paragraph provides examples using the 'cat' and 'echo' commands. It also mentions the less than sign (<) for redirecting standard input, although it is not covered in detail due to limited use cases. Additionally, the paragraph briefly introduces searching for text and files in Linux, hinting at further lessons to explore this topic in depth.

Mindmap

Keywords

πŸ’‘Docker

Docker is an open-source platform designed for building, shipping, and running applications in a consistent manner across various environments. It allows developers to package applications with all of their dependencies into a 'container' that can be run on any machine with Docker installed. The video's theme revolves around teaching Docker from basics to advanced concepts, emphasizing its importance in software development workflows and its role in ensuring consistency across different machines.

πŸ’‘Containers

Containers, in the context of this video, refer to lightweight, standalone, and executable software packages that include everything needed to run an application as a single unit. They are an alternative to traditional virtual machines and are used to isolate applications from each other and the underlying system. Containers are a core concept in Docker, as they enable the easy deployment and scaling of applications.

πŸ’‘Virtual Machines (VMs)

Virtual Machines, or VMs, are software emulations of physical computers that allow you to run multiple operating systems on a single physical machine. The video contrasts VMs with Docker containers, highlighting that VMs are heavyweight and require a full copy of an operating system for each instance, making them slower and more resource-intensive than containers.

πŸ’‘Docker Hub

Docker Hub is a cloud-based registry service that allows users to link code repositories, build images, and share them with others. It serves as a library of pre-built images that can be used as base images for Docker containers. In the video, Docker Hub is mentioned as a place to store and share Docker images, central to the process of distributing containerized applications.

πŸ’‘Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. It is used to automate the creation of Docker images. In the video, the Dockerfile is introduced as a fundamental part of the Docker workflow, providing the instructions needed to build an image of an application.

πŸ’‘Docker Engine

The Docker Engine is the backend of the Docker system that creates and manages Docker objects, such as images, containers, and networks. It is the core component that interacts with the host system to create and manage containers. The video script explains that the Docker Engine is responsible for building and running Docker containers.

πŸ’‘Linux Distributions

Linux distributions, or distros, are diverse versions of the Linux operating system, each designed for different use cases and with varying pre-installed software packages. In the video, different Linux distributions are mentioned as the base for Docker images, such as Ubuntu and Alpine, with the latter being highlighted for its small size, which results in smaller Docker images.

πŸ’‘Docker CLI

Docker CLI (Command Line Interface) is the primary way users interact with Docker. It allows users to run commands to manage Docker objects, such as building images, running containers, and pushing them to Docker Hub. The video script includes various Docker CLI commands that are essential for the Docker workflow.

πŸ’‘Development Workflow

The development workflow in the context of the video refers to the process of developing, building, and deploying applications using Docker. It involves Dockerizing applications, creating Docker images, running containers, and using Docker Hub for distribution. The video emphasizes the efficiency and consistency that Docker brings to the development workflow.

πŸ’‘Linux File System

The Linux file system is a hierarchical organization of files and directories that starts from the root directory ('/'). It includes standard directories like /bin, /boot, /dev, /etc, /home, /root, /lib, /var, and /proc. The video script discusses navigating and manipulating the Linux file system, which is essential for users working with Docker, especially when managing files and directories within containers.

Highlights

Introduction to Docker course covering basics to advanced concepts for software development workflows.

No prior knowledge of Docker needed; three months of programming experience recommended.

Course emphasizes practical learning through active participation and note-taking.

Overview of Docker: Platform for building, running, and shipping applications consistently across different environments.

Comparison of Docker containers and virtual machines, highlighting Docker's lightweight and efficient architecture.

Explanation of Docker's client-server architecture and how containers share the host's operating system kernel.

Step-by-step guide to installing Docker on various operating systems, including system requirements and potential issues.

Development workflow with Docker: Creating Dockerfiles, building images, and running containers.

Practical example of creating a Dockerfile and building a Docker image for a simple Node.js application.

Using Docker Hub to share Docker images and run them on different machines.

Introduction to the Linux command line and basic Linux commands relevant to Docker.

Navigating the Linux file system and understanding standard directories.

Creating, renaming, and deleting files and directories using Linux commands.

Editing and viewing files with text editors like Nano and commands like cat, more, less, head, and tail.

Understanding and utilizing input/output redirection in Linux.

Transcripts

play00:01

welcome to the ultimate docker course in

play00:03

this course i'm going to take you on a

play00:05

journey and teach you everything you

play00:07

need to know about docker from the

play00:08

basics to more advanced concepts so by

play00:10

the end of this course you'll be able to

play00:12

use it like a pro as part of your

play00:14

software development workflow if you're

play00:16

looking for a comprehensive and highly

play00:18

practical course that takes you from

play00:19

zero to hero this is the docker course

play00:22

for you we're gonna start off with a

play00:23

really simple project so you understand

play00:25

the basics then we'll use docker to run

play00:28

and deploy a full stack application with

play00:30

a front end back end and a database so

play00:32

you learn all the necessary techniques

play00:34

and apply them to your own projects i'm

play00:36

ash hamadani and i've taught millions of

play00:38

people how to advance their software

play00:40

engineering skills through my youtube

play00:42

channel and online

play00:43

onlineschoolcodewithmash.com if you're

play00:45

new here be sure to subscribe as we

play00:47

upload new videos all the time

play00:49

now let's jump in and get started

play00:55

[Music]

play01:02

let's quickly talk about what you need

play01:03

to know to take this course to take this

play01:05

course you don't need any prior

play01:07

knowledge of docker because i'm going to

play01:08

teach you everything from scratch but

play01:10

you should have at least three months of

play01:12

programming experience ideally you

play01:14

should have built at least one

play01:15

application so you should know concepts

play01:17

like front-end back-end api and database

play01:20

it doesn't matter what languages and

play01:22

frameworks or what database engines

play01:24

you're familiar with but you should know

play01:25

what these concepts are all about also

play01:28

you should be familiar with basic git

play01:30

commands like cloning a github

play01:31

repository committing code pushing and

play01:34

pulling just the basics nothing more

play01:36

with all that let's move on to the next

play01:38

lesson

play01:40

[Music]

play01:44

now we all have different ways of

play01:46

learning things but let me tell you what

play01:48

i think is the best way to take this

play01:50

course this course is highly practical

play01:52

so you should be active while watching

play01:54

this course in my opinion it's best if

play01:57

you watch each lesson take some notes

play01:59

you can either take notes on a piece of

play02:00

paper or using your favorite note-taking

play02:03

tool just write some keywords some

play02:05

keywords that help you remember what we

play02:07

talked about then after each lesson go

play02:10

through your notes and repeat the same

play02:11

steps i have taken in that lesson so if

play02:14

i talked about a bunch of commands play

play02:15

with those commands make sure you

play02:17

understand how they work if you follow

play02:19

that i promise you by the end of this

play02:21

course you're gonna master docker and

play02:23

use it like a pro

play02:28

alright our journey to master docker

play02:30

begins here in every section you're

play02:32

going to discover something new about

play02:34

docker in this section we're going to

play02:36

talk about what docker is and why it's

play02:38

so popular then we're going to talk

play02:40

about virtual machines and containers

play02:42

next we're going to talk about the

play02:43

architecture of docker so you understand

play02:45

how it works then we're going to install

play02:47

docker and get our hands dirty i'm going

play02:50

to give you an overview of your

play02:51

development workflow with docker and

play02:53

then we're going to see that in action

play02:55

using a really simple example so by the

play02:57

end of this section you will have an

play02:59

idea what docker is all about i'm so

play03:01

excited about this section i hope you

play03:02

are too so let's jump in and get started

play03:05

[Music]

play03:12

[Music]

play03:16

so what is docker and why is everyone

play03:18

using it these days well docker is a

play03:20

platform for building running and

play03:23

shipping applications in a consistent

play03:25

manner so if your application works on

play03:27

your development machine it can run and

play03:29

function the same way on other machines

play03:31

if you have been developing software for

play03:33

a while you've probably come across this

play03:35

situation where your application works

play03:36

on your development machine but doesn't

play03:39

somewhere else can you think of three

play03:40

reasons why this happens

play03:42

well this can happen if one or more

play03:44

files are not included as part of your

play03:46

deployment so your application is not

play03:48

completely deployed it's missing

play03:50

something this can also happen if the

play03:53

target machine is running a different

play03:55

version of some software that your

play03:56

application needs let's say your

play03:58

application needs node version 14 but

play04:00

the target machine is running node

play04:02

version 9. this can also happen if the

play04:04

configuration settings like environment

play04:06

variables are different across these

play04:08

machines and this is where docker comes

play04:10

to the rescue with docker we can easily

play04:13

package up our application with

play04:15

everything it needs and run it anywhere

play04:17

on any machine with docker so if your

play04:19

application needs a given version of

play04:21

node and mongodb all of these will be

play04:23

included in your applications package

play04:25

now you can take this package and run it

play04:28

on any machine that runs docker so if it

play04:30

works on your development machine it's

play04:32

definitely going to work on your test

play04:33

and production machines now there's more

play04:36

if someone joins your team they don't

play04:38

have to spend half a day or so setting

play04:40

up a new machine to run your application

play04:42

they don't have to install and configure

play04:44

all these dependencies they simply tell

play04:46

docker to bring up your application and

play04:48

docker itself will automatically

play04:50

download and run these dependencies

play04:52

inside an isolated environment called a

play04:55

container and this is the beauty of

play04:57

docker this isolated environment allows

play04:59

multiple applications use different

play05:02

versions of some software side by side

play05:04

so one application may use node version

play05:06

14 another application may use node

play05:09

version 9. both these applications can

play05:11

run side by side on the same machine

play05:13

without messing with each other so this

play05:15

is how docker allows us to consistently

play05:17

run an application on different machines

play05:20

now there is one more benefit here when

play05:22

we're done with this application and

play05:24

don't want to work on it anymore we can

play05:26

remove the application and all its

play05:27

dependencies in one go without docker as

play05:30

we work on different projects our

play05:32

development machine gets cluttered with

play05:34

so many libraries and tools that are

play05:36

used by different applications and then

play05:38

after a while we don't know if we can

play05:39

remove one or more of these tools

play05:41

because we're always afraid that we

play05:43

would mess up with some application with

play05:45

docker we don't have to worry about this

play05:47

because each application runs with its

play05:49

dependencies inside an isolated

play05:51

environment

play05:52

we can safely remove an application with

play05:54

all its dependencies to clean up our

play05:56

machine isn't that great

play05:58

so in a nutshell docker helps us

play06:00

consistently build run and ship our

play06:03

applications and that's why a lot of

play06:05

employers are looking for people with

play06:07

darker skills these days so if you're

play06:09

pursuing a job as a software or devops

play06:11

engineer i highly encourage you to learn

play06:13

docker and learn it well and that's

play06:15

exactly what this course is all about

play06:17

i'm going to take you on a journey and

play06:19

teach you everything you need to know

play06:20

about docker so you can use it like a

play06:22

pro no copy pasting commands here

play06:27

[Music]

play06:31

so in the last lesson i briefly talked

play06:32

about containers a container is an

play06:35

isolated environment for running an

play06:37

application now one of the questions

play06:38

that often comes up is how are

play06:40

containers different from virtual

play06:42

machines or vms do you know the

play06:44

differences well a virtual machine as

play06:46

the name implies is an abstraction of a

play06:49

machine or physical hardware so we can

play06:51

run several virtual machines on a real

play06:53

physical machine for example we can have

play06:56

a mac and on this mac we can run two

play06:58

virtual machines one running windows the

play07:00

other running linux how do we do that

play07:03

using a tool called hypervisor i know

play07:05

it's one of those computer science names

play07:07

in simple terms a hypervisor is software

play07:10

we use to create and manage virtual

play07:12

machines there are many hypervisors

play07:14

available out there like virtualbox and

play07:16

vmware which are cross-platform so they

play07:19

can run on windows mac os and linux and

play07:22

hyper-v which is only for windows so

play07:24

with a hypervisor we can manage virtual

play07:26

machines now what is the benefit of

play07:28

building virtual machines well for us

play07:31

software developers we can run an

play07:33

application in isolation inside a

play07:35

virtual machine so on the same physical

play07:37

machine we can have two different

play07:39

virtual machines each running a

play07:41

completely different application and

play07:43

each application has the exact

play07:45

dependencies it needs so application 1

play07:48

may use node version 14 and mongodb

play07:50

version 4 while application 2 may use

play07:53

node version 9 and mongodb version 3.

play07:56

all these are running on the same

play07:58

machine but in different isolated

play08:00

environments that's one of the benefits

play08:02

of virtual machines but there are a

play08:03

number of problems with this model each

play08:06

virtual machine needs a full copy of an

play08:08

operating system that needs to be

play08:09

licensed patched and monitored and

play08:12

that's why these virtual machines are

play08:13

slow to start because the entire

play08:15

operating system has to be loaded just

play08:17

like starting your computer

play08:19

another problem is that these virtual

play08:21

machines are resource intensive because

play08:23

each virtual machine takes a slice of

play08:25

the actual physical hardware resources

play08:28

like cpu memory and disk space so if you

play08:31

have 8 gigabytes of memory that memory

play08:33

has to be divided between different

play08:35

virtual machines of course we can decide

play08:37

how much memory to allocate to each

play08:39

virtual machine but at the end of the

play08:41

day we have a limit in terms of the

play08:43

number of vms we can run on a machine

play08:45

usually a handful otherwise we're going

play08:47

to run out of hardware resources now

play08:49

let's talk about containers containers

play08:51

give us the same kind of isolation so we

play08:54

can run multiple applications in

play08:55

isolation but they're more lightweight

play08:58

they don't need a full operating system

play09:00

in fact all containers on a single

play09:02

machine share the operating system of

play09:04

the host so that means we need to

play09:06

license patch and monitor a single

play09:09

operating system

play09:10

also because the operating system has

play09:12

already started on the host a container

play09:15

can start up pretty quickly usually in a

play09:17

second sometimes less and also these

play09:20

containers don't need a slice of the

play09:22

hardware resources on the host so we

play09:24

don't need to give them a specific

play09:25

number of cpu cores or a slice of memory

play09:28

or disk space so on a single host we can

play09:31

run tens or even hundreds of containers

play09:34

side by side so these are the

play09:36

differences between containers and

play09:37

virtual machines

play09:41

[Music]

play09:45

let's talk about the architecture of

play09:46

docker so you understand how it works

play09:49

docker uses a client server architecture

play09:51

so it has a client component that talks

play09:53

to a server component using a restful

play09:56

api

play09:56

the server also called the docker engine

play09:59

sits on the background and takes care of

play10:01

building and running docker containers

play10:04

but technically a container is just a

play10:06

process like other processes running on

play10:08

your computer but it's a special kind of

play10:10

process which we're going to talk about

play10:12

soon now as i told you unlike virtual

play10:14

machines containers don't contain a

play10:17

full-blown operating system instead all

play10:20

containers on a host share the operating

play10:22

system of the host now more accurately

play10:25

all these containers share the kernel of

play10:27

the host what's a kernel a kernel is the

play10:30

core of an operating system it's like

play10:32

the engine of a car it's the part that

play10:34

manages all applications as well as

play10:36

hardware resources like memory and cpu

play10:39

every operating system has its own

play10:41

kernel or engine and these kernels have

play10:43

different apis that's why we cannot run

play10:46

a windows application on linux because

play10:48

under the hood this application needs to

play10:50

talk to the kernel of the underlying

play10:52

operating system okay

play10:54

so that means on a linux machine we can

play10:57

only run linux containers because these

play10:59

containers need linux on a windows

play11:02

machine however we can run both windows

play11:04

and linux containers because windows 10

play11:06

is now shipped with a custom built linux

play11:09

kernel this is in addition to the

play11:11

windows kernel that's always been in

play11:13

windows it's not a replacement so with

play11:15

this linux kernel now we can run linux

play11:17

applications natively on windows so on

play11:20

windows we can run both linux and

play11:22

windows containers our windows

play11:24

containers share the windows kernel and

play11:27

our linux containers share the linux

play11:29

kernel okay

play11:30

now what about mac os well mac os has

play11:33

its own kernel which is different from

play11:35

linux and windows kernels and this

play11:37

kernel does not have native support for

play11:40

continuous applications so docker on mac

play11:43

uses a lightweight linux virtual machine

play11:46

to run linux containers all right enough

play11:48

about architecture next we're going to

play11:50

install docker and that's where the fun

play11:52

begins

play11:55

[Music]

play11:59

all right now let's install the latest

play12:00

version of docker if you have an

play12:02

existing version of docker on your

play12:03

machine i highly encourage you to

play12:05

upgrade to the latest version because

play12:07

your version might be old and not

play12:09

compatible with the version i'm using in

play12:11

this course so here i'm using docker

play12:13

version

play12:16

20.10.5 okay

play12:18

so to get docker

play12:20

go to this page docs.docker.com

play12:24

get docker or you can just google get

play12:26

docker or install docker now on this

play12:28

page you can see instructions for

play12:30

downloading and installing docker

play12:32

desktop for mac and windows as well as

play12:35

docker engine for linux so on mac and

play12:37

windows we have docker desktop which is

play12:40

the combination of docker engine plus a

play12:42

bunch of other tools at the time of

play12:44

recording this we don't have docker

play12:45

desktop for linux we only have the

play12:47

docker engine but of course that might

play12:49

change in the future

play12:50

so a couple of notes for my mac and

play12:53

windows users let's go to this page

play12:56

so over here you can download the latest

play12:58

version from docker hub but before doing

play13:01

this i highly encourage you to go

play13:03

through a system requirement and make

play13:05

sure your computer satisfies these

play13:07

requirements otherwise you might

play13:09

encounter weird issues so the

play13:11

installation is pretty straightforward

play13:13

when you go to this page you're going to

play13:15

download a dmg file just drag and drop

play13:17

this onto the applications folder and

play13:19

then start it this is very important a

play13:22

lot of people miss that step so when you

play13:24

start docker engine

play13:26

by double clicking on this application

play13:28

you're going to see the docker icon on

play13:30

the top status bar

play13:32

if you don't see this docker engine is

play13:34

not running and there is nothing you can

play13:35

do so you have to wait for this to start

play13:38

the same applies to my windows users so

play13:41

back to this page

play13:43

let's look at the instructions for

play13:44

windows

play13:46

so once again you can download the

play13:47

latest version from docker hub but once

play13:50

again make sure to read system

play13:52

requirements one of the things that is

play13:54

really important is enabling hyper-v

play13:57

and containers windows features so i

play13:59

don't have a windows machine to show you

play14:01

here but just go to the page where you

play14:03

can turn on or turn off windows features

play14:05

there make sure you have enabled hyper-v

play14:08

and containers

play14:09

it's pretty straightforward but if you

play14:11

can't find it just google it i'm pretty

play14:13

sure there are tons of articles out

play14:15

there

play14:15

also at the end of the installation you

play14:17

might get an error saying ws2

play14:19

installation is incomplete basically

play14:22

what this error is saying is that you

play14:24

need to upgrade the linux kernel that is

play14:26

shipped with your windows so just click

play14:28

on this link

play14:29

this is going to take you to this page

play14:30

on microsoft.com where you can download

play14:33

the latest linux kernel so just click on

play14:35

this link to get an msi file run it and

play14:38

then you have to restart your computer

play14:40

now once your computer is restarted you

play14:42

need to wait a little while until docker

play14:44

engine started depending on your machine

play14:47

this might take several seconds or up to

play14:48

a minute

play14:50

so wait a little while then open up a

play14:52

terminal window and run docker

play14:55

version

play14:56

so over here

play14:58

you can see the version of the client

play15:00

and the server if the server which is

play15:03

docker engine is not running you are not

play15:06

going to see this information and this

play15:07

applies to all operating systems windows

play15:10

mac os and linux so make sure docker

play15:13

engine is running before going forward

play15:15

if you encounter any errors you can post

play15:17

your question on our forum at

play15:19

forum.codewithmosh.com

play15:21

or if you want a faster response just

play15:23

google the error message i'm pretty sure

play15:25

hundreds of people have encountered the

play15:26

same problem as you

play15:30

[Music]

play15:34

now let's talk about your development

play15:35

workflow when using docker so to start

play15:38

off we take an application it doesn't

play15:40

matter what kind of application it is or

play15:42

how it's built we take that application

play15:44

and dockerize it which means we make a

play15:47

small change so that it can be run by

play15:49

docker how we just add a docker file to

play15:52

it a docker file is a plain text file

play15:54

that includes instructions that docker

play15:56

uses to package up this application into

play15:59

an image this image contains everything

play16:01

our application needs to run everything

play16:04

typically a cut down operating system a

play16:07

runtime environment like node or python

play16:09

it also contains application files

play16:11

third-party libraries environment

play16:13

variables and so on so we create a

play16:15

docker file and give it to docker for

play16:18

packaging our application into an image

play16:20

once we have an image we tell docker to

play16:22

start a container using that image so a

play16:25

container as i told you is just a

play16:27

process but it's a special kind of

play16:28

process because it has its own file

play16:31

system which is provided by the image so

play16:33

our application gets loaded inside a

play16:35

container or a process and this is how

play16:37

we run our application locally on our

play16:40

development machine so instead of

play16:41

directly launching the application and

play16:44

running it inside a typical process we

play16:46

tell docker to run it inside a container

play16:49

an isolated environment now here's the

play16:51

beauty of docker once we have this image

play16:54

we can push it to a docker registry like

play16:56

docker hop docker hub to docker is like

play16:58

github to git it's a storage for docker

play17:01

images that anyone can use so once our

play17:03

application image is on docker hub then

play17:05

we can put it on any machines running

play17:07

docker this machine has the same image

play17:09

we have on our development machine which

play17:11

contains a specific version of our

play17:13

application with everything it needs so

play17:16

we can start our application the same

play17:18

way we started it on our development

play17:19

machine we just tell docker to start a

play17:22

container using this image so with

play17:24

docker we no longer need to maintain

play17:26

long complex release documents that have

play17:29

to be precisely followed all the

play17:31

instructions for building an image of an

play17:33

application are written in a docker file

play17:35

with that we can package up our

play17:37

application into an image and run it

play17:39

virtually anywhere this is the beauty of

play17:41

docker next we're going to see this

play17:42

workflow in action

play17:46

[Music]

play17:50

in this design i'm going to walk you

play17:52

through a typical development workflow

play17:54

now don't try to memorize anything in

play17:55

this lesson because i just want you to

play17:57

see the big picture so everything i show

play17:59

you in this lesson we're going to cover

play18:01

in depth later in the course so here in

play18:03

this terminal window i'm currently on my

play18:05

desktop i'm going to create a new

play18:07

directory called hello

play18:09

docker

play18:10

then go inside this directory and open

play18:13

it in visual studio code

play18:16

so i'm going to use vs code as my editor

play18:18

but you can use any editor that you

play18:20

prefer okay

play18:21

now in this directory i'm going to add a

play18:23

new file called

play18:25

app.js you're going to write one line of

play18:28

javascript code you don't have to be a

play18:29

javascript developer you don't even need

play18:31

to learn javascript so just follow along

play18:34

with me

play18:34

so here we're going to write console in

play18:36

lowercase dot log

play18:39

hello

play18:41

so with this we're going to print a

play18:43

message on the terminal let's say this

play18:44

is an application and we want to

play18:46

dockerize this application so we want to

play18:48

build run and ship it using docker so

play18:51

typically without docker if you want to

play18:53

ship this application or more accurately

play18:56

this program to a different computer on

play18:58

that computer we need to install node

play19:00

and then we can go to the terminal and

play19:02

type node app.js so we get the output so

play19:06

here are the instructions for deploying

play19:08

this program we need to start with an

play19:10

operating system then we need to install

play19:12

node which is an execution environment

play19:14

for javascript code

play19:15

next we need to copy our application

play19:17

files and finally we need to run node

play19:20

app.js so we have to follow four steps

play19:23

just for a simple program what if you

play19:25

were working with a really complex

play19:26

application you would end up with a

play19:28

complex release document that had to be

play19:30

precisely followed now this is where

play19:33

docker comes to the rescue we can write

play19:35

these instructions inside a docker file

play19:38

and let docker package up our

play19:40

application

play19:41

so back to vs code we're going to add

play19:44

another file to this project called

play19:46

docker file so capital d and all the

play19:49

other letters are lowercase and this

play19:51

file doesn't have any extensions

play19:53

okay

play19:54

now vs code is asking if you want to

play19:56

install the recommended extensions for

play19:58

docker we can go ahead with that

play20:01

good

play20:03

so back to this docker file here we

play20:05

write instructions for packaging our

play20:06

application so typically we start from a

play20:09

base image this base image has a bunch

play20:12

of files we're going to take those files

play20:14

and add additional files to it this is

play20:16

kind of like inheritance in programming

play20:18

okay so what is the base image well we

play20:21

can start from a linux image and then

play20:24

install node on top of it

play20:26

or we can start from a node image this

play20:29

image is already built on top of linux

play20:31

now how do i know these names well these

play20:33

images are officially published on

play20:36

docker hub

play20:38

so if you go to

play20:39

hub.docker.com and search for node you

play20:41

can see the official node image here so

play20:44

docker hub is a registry for docker

play20:46

images now back to our docker file so we

play20:49

start from a node image now if you look

play20:51

at docker hop you will see that there

play20:53

are multiple node images these node

play20:56

images are built on top of different

play20:58

distributions of linux so linux has

play21:00

different distributions or different

play21:02

flavors used for different purposes

play21:05

now here we can specify a tag using a

play21:07

column to specify which linux

play21:09

distribution we want to use for this

play21:11

demo i'm going to use alpine which is a

play21:14

very small linux distribution so the

play21:16

size of the image that we're going to

play21:18

download and build on top of is going to

play21:20

be very small okay

play21:22

so we start from that image then we need

play21:24

to copy our application or program files

play21:27

for that we use the copy instruction or

play21:29

copy command

play21:30

we're going to copy all the files in the

play21:32

current directory

play21:34

into the app directory into that image

play21:37

so that image has a file system and in

play21:39

that file system we're going to create a

play21:41

directory called app okay now finally

play21:44

we're going to use the command

play21:46

instruction to execute a command what

play21:49

command should we execute here

play21:51

node

play21:52

app.js but this file is inside the app

play21:55

directory so we have to prefix it with

play21:58

the directory name alternatively

play22:01

here we could set the current working

play22:03

directory work dir to slash app

play22:07

and then we don't need to prefix this

play22:10

with the directory name so when we use

play22:12

this instruction all the following

play22:13

instructions assume that we're currently

play22:15

inside the app directory okay

play22:18

so

play22:19

these instructions clearly document our

play22:22

deployment process

play22:23

now

play22:24

we go to the terminal and tell docker to

play22:27

package up our application so we say

play22:29

docker build we need to give our image a

play22:32

tag a tag to identify so

play22:35

t

play22:36

here we specify a name like hello docker

play22:39

and then we need to specify where docker

play22:41

can find a docker file so we're

play22:44

currently inside hello docker directory

play22:46

and our docker file is right here so we

play22:49

use a period to reference the current

play22:51

directory

play22:52

let's go with that

play22:54

now you might be expecting an image file

play22:57

inside the current directory but back in

play22:59

vs code look there is nothing here

play23:01

because the image is not stored here and

play23:03

in fact an image is not a single file

play23:06

how docker stores this image is very

play23:08

complex and we don't have to worry about

play23:09

it so back to the terminal to see all

play23:12

the images on this computer we type

play23:14

docker images or

play23:17

docker image ls which is short for list

play23:20

so take a look

play23:22

on this machine we have a repository

play23:24

called hello docker

play23:26

in this repository we have an image with

play23:28

this tag latest so docker added this by

play23:31

default we'll talk about this later in

play23:33

the course but basically we use these

play23:35

tags for versioning our images so each

play23:37

image can contain a different version of

play23:40

our application okay

play23:41

now each image also has a unique

play23:44

identifier

play23:45

here we can see when the image was

play23:47

created and the size of this image so

play23:49

because we used node from linux alpine

play23:52

we ended up with 112 megabytes of data

play23:56

in this image so this image contains

play23:58

alpine linux node and our application

play24:01

files and the total size is 112

play24:04

megabytes now if we used a different

play24:06

node image that was based on a different

play24:08

distribution of linux we would end up

play24:10

with a larger image and then when

play24:12

deploying that image we would have to

play24:13

transfer that image from one computer to

play24:15

another so that's why we use node alpine

play24:18

because this is a very small image okay

play24:20

so we have built this image now we can

play24:22

run this image on any computer running

play24:24

docker so on this machine which is my

play24:26

development machine i can say docker run

play24:30

and then type the image name hello

play24:32

docker and it doesn't matter which

play24:34

directory i'm in because this image

play24:36

contains all the files for running our

play24:38

application

play24:39

now look

play24:41

we see the message on the terminal now i

play24:43

can go ahead and publish this image to

play24:45

docker hub so anyone can use this image

play24:47

then i can go on another machine like a

play24:49

test or a production machine and pull

play24:52

and run this image in fact i've done

play24:53

this before recording this video

play24:56

so back to docker hub look i have this

play24:58

repository code with mosh slash hello

play25:01

docker now we can take this image and

play25:03

run it on any computers let me show you

play25:05

something really cool so

play25:07

let's search for play with docker

play25:10

let's go to this page

play25:12

and log in

play25:16

here we need to sign in with our docker

play25:17

id anyone can create this id on

play25:19

docker.com

play25:20

so let's go ahead

play25:23

and start a lab

play25:26

over here we can start a new virtual

play25:28

machine so let's add a new instance

play25:32

now this virtual machine is a black

play25:34

machine it only has an operating system

play25:36

which is linux and docker so in this

play25:39

terminal window

play25:41

if we type node

play25:43

look node command not found so node is

play25:46

not installed here but because we have

play25:48

docker we can pull and run the image

play25:50

that i published on docker hub so let me

play25:53

maximize this window by pressing alt and

play25:56

enter

play25:57

first let's run docker version

play26:02

so on this machine we're running docker

play26:03

version 20.10.0

play26:07

so to pull and run my program first we

play26:10

type docker pool

play26:12

code with mosh slash hello dash docker

play26:18

alright docker downloaded this image we

play26:20

can verify it by typing docker what

play26:23

command should be run here docker images

play26:25

or

play26:26

image ls

play26:29

so on this machine we have this

play26:30

repository code with mosh slash hello

play26:33

docker and this repository contains a

play26:35

single image with this tag latest so now

play26:38

we can run this application exactly the

play26:40

same way we run it on my development

play26:42

machine so from any directory we can

play26:45

type docker run

play26:47

code with mosh slash hello dash docker

play26:53

and here's the message beautiful of

play26:55

course i had to cut this down in editing

play26:57

it took a little while to start this

play26:58

application on this very slow virtual

play27:00

machine but you got the point

play27:02

so we can take any application and

play27:05

dockerize it by adding a docker file to

play27:07

it this docker file contains

play27:09

instructions for packaging an

play27:11

application into an image once we have

play27:13

an image we can run it virtually

play27:14

anywhere on any machine with docker

play27:19

hey guys mosh here if you're serious

play27:20

about learning docker i actually have a

play27:22

complete docker course on my coding

play27:24

school codewithmosh.com this tutorial

play27:27

you've been watching is a cut down

play27:29

version of the complete course in the

play27:31

full course you will learn how to run

play27:33

and deploy a full stack application with

play27:35

a front-end back-end and a database our

play27:38

application also includes automated

play27:39

tests so it's a great real-world

play27:42

scenario for learning docker just like

play27:44

my other courses you will also receive a

play27:46

30-day money-back guarantee and a

play27:47

certificate of completion you can add

play27:49

your resume in case you're interested

play27:51

you can find the link down below in the

play27:53

description box

play27:59

all right the next step in our journey

play28:00

is the linux command line but why linux

play28:03

what if you're a windows user well you

play28:05

still need to know a bit of linux for a

play28:06

number of reasons for starters docker

play28:09

has its foundations built on top of

play28:11

basic linux concepts so if you want to

play28:13

be productive and troubleshoot issues

play28:15

easily you need to know some of the

play28:17

basic linux commands also most tutorials

play28:20

online are based on linux commands so if

play28:22

you don't understand these basic

play28:24

commands you're not going to get far in

play28:26

my opinion learning linux is like

play28:28

learning english i think everybody

play28:30

should know some english these days you

play28:31

don't need to speak it or write a book

play28:33

in it but you need to understand it so

play28:35

unless you're a power linux user do not

play28:37

skip this section it's gonna be super

play28:39

easy and extremely useful

play28:41

so let's jump in and get started

play28:45

[Music]

play28:51

let's start up this section by talking

play28:53

about linux distributions also called

play28:56

linux distros

play28:57

so as you probably know linux is open

play29:00

source software and for this reason many

play29:02

individuals and communities have created

play29:04

their own version of linux called linux

play29:06

distributions each of these

play29:08

distributions is made to fit specialized

play29:10

needs like running servers desktop

play29:13

computers mobile phones and so on so we

play29:15

have ubuntu which is one of the most

play29:17

popular linux distributions we also have

play29:20

debian alpine which we briefly talked

play29:23

about it's a very small linux

play29:25

distribution

play29:26

we also have fedora centos and so on

play29:29

believe it or not there are more than a

play29:30

thousand linux distributions out there

play29:32

now most of these distributions support

play29:35

pretty much the same set of commands but

play29:37

sometimes you might discover differences

play29:38

along the way so be aware of that in

play29:41

this section we're going to use ubuntu

play29:43

linux because it's one of the most

play29:45

popular distributions but if you have a

play29:47

preference for another distribution

play29:48

that's totally fine

play29:52

[Music]

play29:56

all right let's see how we can run

play29:57

ubuntu on this machine from this lesson

play30:00

i want you to start taking notes so

play30:02

watch this video take some notes and

play30:04

then after the video repeat the steps i

play30:06

have shown you okay

play30:07

so we go to hub.docker.com

play30:10

and search for ubuntu

play30:12

over here you can see the official

play30:14

ubuntu image that's been downloaded more

play30:16

than 10 million times let's have a quick

play30:18

look here

play30:20

so for each image you can see the

play30:21

command to pull that image onto your

play30:24

machine now in this lesson i'm not going

play30:26

to use the pull command i'm going to

play30:27

show you a shortcut

play30:29

so here in the terminal instead of

play30:31

running docker pool ubuntu

play30:34

i'm going to run docker

play30:36

run ubuntu now if we have this image

play30:39

locally docker is going to start a

play30:41

container with this image otherwise it's

play30:43

going to pull this image behind the

play30:45

scene and then start a container so take

play30:47

a look

play30:48

see

play30:49

docker is unable to find this image

play30:51

locally and now it's pulling it from

play30:53

docker hub

play30:55

now what happened well docker started a

play30:57

container but because we didn't interact

play30:59

with this container the container

play31:01

stopped let me prove this to you so if

play31:04

you run docker ps we can see the list of

play31:07

running processes or running containers

play31:10

look we don't have any containers

play31:12

running here but if we type docker ps

play31:15

dash a for all

play31:17

we can see the stopped containers as

play31:19

well let me increase the size of this

play31:21

window so you can see it clearly good

play31:24

so we have two stopped containers the

play31:26

first one

play31:27

is using the ubuntu image this is the

play31:29

one that we just started and the second

play31:31

one is hello docker which we started

play31:34

earlier in the course so to start a

play31:36

container and interact with it we have

play31:38

to type docker run dash i t that is

play31:42

short for interactive we're going to

play31:44

start a container in the interactive

play31:45

mode

play31:46

and in this container we're going to

play31:47

load the ubuntu image which we have

play31:50

locally

play31:52

good now what we have here is called the

play31:54

shell a shell is a program that takes

play31:56

our commands and passes them to the

play31:58

operating system for execution okay now

play32:00

what we have here is called the shell

play32:03

prompt let me break it down for you so

play32:05

it doesn't look mysterious the first

play32:07

part root

play32:08

represents the currently logged in user

play32:10

so by default i'm logged in as the root

play32:12

user which has the highest privileges

play32:15

then after the add sign we have the name

play32:17

of the machine

play32:18

so this container has this id which is

play32:21

automatically generated by docker and in

play32:24

this case it's like the name of a

play32:25

machine okay and after the colon

play32:28

you can see forward slash that

play32:30

represents where we are in the file

play32:32

system a forward slash represents the

play32:35

root directory that is the highest

play32:37

directory in the file system we'll talk

play32:39

about that soon then we have a pound

play32:41

and this means i have the highest

play32:43

privileges because i've logged in as the

play32:45

root user if i logged in as a normal

play32:47

user instead of a pound we would see a

play32:50

dollar sign okay

play32:51

so in this shell we can execute a bunch

play32:54

of commands for example we can say echo

play32:56

hello

play32:57

and this prints hello on the terminal we

play32:59

can also say who am i

play33:02

this shows the current user

play33:04

so these commands that we're running

play33:05

here this shell program that i told you

play33:07

about takes these commands and passes

play33:09

them to the kernel for execution now let

play33:11

me show you something really cool if we

play33:13

type echo dollar sign 0 we can see the

play33:17

location of this shell program take a

play33:19

look

play33:20

so

play33:21

that is forward slash bin slash bash so

play33:24

bin is a folder or a directory and

play33:27

inside this directory we have a program

play33:29

called bash which is short for born

play33:32

again shell so apparently steve bourne

play33:34

is the first person who created a shell

play33:36

program bash or born again shell is a

play33:39

reference to steve bourne so bash is an

play33:42

enhanced version of the original shell

play33:44

program okay

play33:45

now one thing you probably notice is

play33:47

that in linux we use a forward slash to

play33:49

separate files and directories but in

play33:52

windows we use a backslash so that's one

play33:54

of the first differences

play33:56

the other difference is that linux is a

play33:58

case sensitive operating system so if

play34:00

you type echo with a capital e

play34:03

it's not going to work bash tells us

play34:05

echo command not found so lowercase and

play34:08

uppercase letters are different and this

play34:10

is not limited to commands it's

play34:12

applicable everywhere if you want to

play34:13

reference a file or a directory or a

play34:15

user pretty much anything we should

play34:17

always spell it properly with the right

play34:20

uppercase and lowercase letters now one

play34:22

last thing for this lesson using the up

play34:25

and down arrows

play34:27

we can go through the commands we have

play34:28

executed so far so this is a pretty

play34:30

useful shortcut you need to know because

play34:32

you don't want to type these commands

play34:33

manually all the time okay

play34:35

also using the history command we can

play34:37

see all the commands we have executed

play34:39

lately so take a look so earlier we used

play34:42

who am i we also used echo and so on

play34:45

now we can replay any of this command by

play34:47

typing an exclamation mark followed by

play34:49

the command number so if i type 2

play34:52

this is exactly like running who am i

play34:55

now it's your turn i want you to pause

play34:57

the video go through your note and

play34:59

execute the commands i have shown you in

play35:00

this video

play35:04

[Music]

play35:08

these days most operating systems and

play35:10

development platforms come with a

play35:12

package manager you've probably worked

play35:14

with tools like npm yarn pip and so on

play35:18

here in ubuntu we also have a package

play35:20

manager called apt which is short for

play35:22

advanced package tool so let's execute

play35:25

this command

play35:26

look

play35:27

this command has a bunch of sub-commands

play35:30

so using list we can see the list of

play35:32

packages we can also search for them we

play35:34

can show details about the package we

play35:36

can install reinstall and remove a

play35:38

package and so on

play35:40

now technically apt is the newer package

play35:42

manager we also have apt-get which you

play35:45

see a lot in online tutorials going

play35:48

forward we're going to use apt because

play35:49

it's easier to work with

play35:51

so let's say we want to install a

play35:53

package called nano nano is a basic text

play35:56

editor for linux now if you press enter

play35:58

here we get an error because this image

play36:01

this ubuntu image we are running does

play36:03

not have nano so this is where we use

play36:05

apt to install this package

play36:07

so if we type apt install nano

play36:11

we get an error saying unable to locate

play36:13

package nano why is this happening well

play36:16

here in linux we have a package database

play36:18

and this database might contain hundreds

play36:20

of packages but not all these packages

play36:23

are installed so if you want to see all

play36:25

the packages in this database we type

play36:27

apt list

play36:30

look these are all the packages now in

play36:32

front of these packages you can see some

play36:34

of them are installed but not all

play36:36

packages in this database are installed

play36:38

when we type app install nano

play36:41

this command looks at the package

play36:43

database and in this database it cannot

play36:45

find a package called nano so this is

play36:48

where

play36:49

we use the update command to update the

play36:51

package database

play36:52

now let me press enter

play36:55

then x is going to all these sources

play36:57

like

play36:58

security.ubuntu.com and all these other

play37:00

websites to download the list of

play37:02

packages

play37:03

so now our package database is updated

play37:05

so if we run apt list

play37:09

we see far more packages and as you can

play37:12

see most of these packages are not

play37:14

installed because we don't have

play37:15

installed in front of them so now we can

play37:17

run apt install nano

play37:21

and

play37:22

nano is installed

play37:24

so here's what you need to take away

play37:26

before installing a package you should

play37:28

always run apt update to update your

play37:30

package database and then

play37:33

you can install a package like nano now

play37:36

we'll talk about nano later in this

play37:37

section but before going forward let's

play37:40

make sure that this package is installed

play37:42

properly so if you type nano great so

play37:45

here we have a text editor we can type

play37:46

something let me resize the window

play37:50

so down below you can see the shortcuts

play37:52

to exit we have to press ctrl and x

play37:56

now it's asking if you want to save the

play37:58

changes no so we're going to press n

play38:01

good we're back here we can clear the

play38:03

terminal window by pressing control and

play38:06

l okay

play38:07

so we have installed nano now let's say

play38:09

we want to remove it so we type apt

play38:12

remove nano

play38:15

it's asking for confirmation let's go

play38:17

ahead

play38:18

great so nano is gone if i type nano

play38:22

we get this error saying no such file or

play38:24

directory

play38:25

now here's a little exercise for you in

play38:27

this image we don't have python so we

play38:30

get an arrow so i want you to use apt to

play38:32

install python in this image make sure

play38:34

it works and then remove it

play38:38

[Music]

play38:43

so in linux just like windows our files

play38:45

and directories are organized in a tree

play38:48

in a hierarchical structure so in

play38:50

windows we have a structure like this

play38:52

with c drive on top of the hierarchy

play38:54

then below that we have directories like

play38:56

program files windows and so on

play38:59

in linux we have the root directory on

play39:01

top of the hierarchy below that we have

play39:03

a bunch of standard directories for

play39:05

example we have bin which includes

play39:07

binaries or programs we have boot which

play39:10

includes all the files related to

play39:12

booting then we have dev the first time

play39:14

i saw this i thought this is short for

play39:16

development it's not it's short for

play39:18

devices so in linux everything is a file

play39:22

including devices directories network

play39:24

sockets pipes and so on so the files

play39:27

that are needed to access devices are

play39:29

stored in this directory then we have

play39:31

etsy there are different opinions what

play39:33

is this short for but one common opinion

play39:36

is this is short for editable text

play39:38

configuration so this is where we have

play39:40

configuration files we also have home

play39:42

this is where home directories for users

play39:44

are stored so on a machine with multiple

play39:47

users each user is going to have a home

play39:49

directory here we also have root which

play39:51

is the home directory of the root user

play39:54

only the root user can access this

play39:55

directory then we have lib which is used

play39:58

for keeping library files like software

play40:00

library dependencies we have var which

play40:02

is short for variable and this is where

play40:04

we have files that are updated

play40:06

frequently like log files application

play40:08

data and so on and finally we have proc

play40:11

which includes files that represent

play40:13

running processes so once again in linux

play40:16

everything is a file processes devices

play40:19

even directories are files now you don't

play40:21

need to memorize any of these

play40:22

directories i just listed them here so

play40:25

as we go through the course these

play40:26

directories look familiar to you that's

play40:28

all next i'm going to show you how to

play40:30

navigate the file system and there you

play40:33

will see these directories one more time

play40:37

[Music]

play40:41

let's see how we can navigate the linux

play40:42

file system so the first command we're

play40:44

going to talk about is pwd which is

play40:47

short for print working directory

play40:50

with this we can see where we are in the

play40:51

file system so a forward slash

play40:54

represents the root directory now to see

play40:57

the files and directories here we type

play40:59

ls which is short for list

play41:01

so we have bin which we talked about

play41:02

earlier that is short for binaries this

play41:04

is where we have binary files and

play41:06

programs we have dev which includes

play41:08

files for devices

play41:10

we have boot which includes boot files

play41:12

etsy which includes configuration files

play41:14

and so on now by default ls lists these

play41:18

items on multiple lines if you don't

play41:20

like this layout and want to show one

play41:22

item per line you need to pass an option

play41:25

that is dash one

play41:27

you might prefer this layout we have

play41:29

another option

play41:31

dash l for seeing a long listing

play41:34

this listing includes more details

play41:36

so in the first column

play41:38

we have the permissions of this file or

play41:40

directory the first time you see this it

play41:42

might look really scary but trust me

play41:44

it's easier than you think we'll talk

play41:46

about that later in this section

play41:48

over here you can see the user that owns

play41:50

this file or directory we can see the

play41:53

size we can see the date and so on now

play41:56

to change the current directory

play41:58

we use the cd command we have the same

play41:59

command in windows now here we can type

play42:02

a relative or an absolute path a

play42:04

relative path is relative to where we

play42:06

are so in this root directory we have

play42:09

directories like bin boot and so on so

play42:11

this is a relative path now in contrast

play42:14

an absolute path always starts from the

play42:16

root directory so let's go to a

play42:18

directory starting with e

play42:20

now we can press tab to get auto

play42:22

completion beautiful now let's go to a

play42:24

directory starting with a now if you

play42:27

press tab nothing happens because we

play42:29

have multiple directories starting with

play42:31

a so we have to press tab one more time

play42:34

great so we have three entries three

play42:36

directories add user.conf alternatives

play42:40

and apt

play42:41

so let's type the second letter p and

play42:44

then press tab

play42:46

beautiful now let's see what we have in

play42:48

this directory

play42:49

so these blue items are directories and

play42:52

sources.less is a file

play42:54

now to get out of this directory

play42:56

we can go one level up by typing two

play42:59

periods

play43:00

again i'm pretty sure you're familiar

play43:01

with this but i want to cover it to make

play43:03

this section comprehensive so we can go

play43:05

one level up to get to the etsy

play43:07

directory

play43:08

or two levels up to get to the root

play43:11

directory look we're currently in the

play43:14

root directory

play43:15

now when using ls we can optionally

play43:18

specify a path let's say i'm somewhere

play43:20

in the file system but i want to look at

play43:22

the content of another directory i don't

play43:24

want to navigate to that directory so

play43:26

i'm currently in the root directory i

play43:28

want to know what files and directories

play43:30

we have inside the bin directory so once

play43:32

again we can type a relative path or

play43:35

an absolute path starting from the root

play43:37

directory

play43:38

so here are the binaries in this

play43:40

directory look at pwd

play43:42

that's the command that we just executed

play43:45

so pwd is a program in this directory

play43:48

here's another example

play43:50

look at

play43:51

echo

play43:52

so most of the commands we have been

play43:53

exploring so far are programs or

play43:56

binaries in the bin directory okay

play43:59

now let me show you a shortcut earlier i

play44:01

told you that here we have

play44:03

this home directory where each user has

play44:06

a home directory but the root user

play44:09

has a special home directory called root

play44:12

now to get here there are two options we

play44:14

can type an absolute or relative path

play44:17

and go to root but there is a shortcut

play44:19

so let me get out of this directory

play44:22

so i'm currently

play44:23

in the root directory

play44:25

to get to my home directory

play44:27

i can type a tilde and this applies to

play44:30

all users not just the root user

play44:32

whenever we type cd tilde we go to our

play44:35

home directory now

play44:37

right now there is nothing here but in

play44:39

the next lesson i will show you how to

play44:40

create some files in this directory

play44:42

so before moving on to the next lesson i

play44:45

want you to spend a couple of minutes

play44:46

and play with the commands we explored

play44:48

in this lesson navigate the file system

play44:50

get adventurous see what you can

play44:52

discover i'll see you in the next lesson

play44:56

[Music]

play45:00

let's see how we can manipulate files

play45:02

and directories so i'm currently in the

play45:04

root directory and i want to go to my

play45:06

home directory how can i do that do you

play45:09

remember we type cd tilde great now

play45:14

in this directory i want to create a

play45:16

directory called test so we type mkdir

play45:19

test

play45:20

let's verify it's right here as i told

play45:23

you before blue represents a directory

play45:26

now let's say we want to rename this

play45:27

directory how do we do that

play45:30

we use the move command with this we can

play45:32

rename files and folders or move them

play45:34

somewhere else so we can move or rename

play45:37

test to docker

play45:41

all right beautiful now let's go in this

play45:43

directory

play45:46

to create a new file

play45:47

we use the touch command so we can

play45:49

create hello.txt

play45:52

look we have this new file here this is

play45:55

a new empty file in the next lesson i

play45:57

will show you how to edit files so for

play45:59

now don't worry about it also

play46:01

using the touch command we can create

play46:03

multiple files in one go so we can

play46:05

create file 1 file 2 and file 3.

play46:10

now take a look beautiful now i've got a

play46:13

question for you how do we list these

play46:15

files with a single file per line do you

play46:17

remember we type ls dash 1.

play46:21

now let's say we want to rename

play46:23

hello.txt to something else once again

play46:26

we can use the move command so we type h

play46:28

press tab to get auto completion we can

play46:31

rename this to hello

play46:33

docker.txt

play46:34

or

play46:35

we can move it to a different directory

play46:37

for example we can move it to the etsy

play46:39

directory and here i'm using an absolute

play46:42

path because my path has started from

play46:44

the root directory

play46:45

now in this lesson i don't want to move

play46:47

this anywhere so i just want to rename

play46:49

this to

play46:50

hello

play46:51

docker.txt and if you're wondering how i

play46:54

remove this entire word in one go i

play46:57

press ctrl and w

play46:59

so let's bring it back hello docker.txt

play47:03

take a look good

play47:05

now let's see how we can remove one or

play47:06

more files to do that

play47:08

use the rm command like here we can type

play47:11

one or more files so we can say

play47:13

file1.txt file2.txt or we can use a

play47:16

pattern for example i can say

play47:19

i want to remove all files that start

play47:23

with file let's verify it

play47:26

beautiful we only have hello docker now

play47:29

let's get out of this directory and

play47:31

remove the directory itself so we type

play47:34

rm

play47:35

docker

play47:37

now we get an error saying docker is a

play47:39

directory so to remove directories we

play47:42

have to use the r option which is short

play47:44

for recursive so we want to remove this

play47:46

directory and all its content

play47:48

recursively

play47:50

now we press d tab beautiful so let's

play47:53

verify there is nothing here

play47:56

so let's quickly recap

play47:57

we use mkdir to create a new directory

play48:00

we use touch to create a new file

play48:03

we use move to move or rename files or

play48:06

directories and we use rm to remove

play48:09

files and directories

play48:10

now as an exercise go to your home

play48:12

directory create a bunch of files and

play48:14

directories rename them and then remove

play48:16

them

play48:17

in the next lesson i'm going to show you

play48:19

how to view and edit files

play48:23

[Music]

play48:27

all right let's see how we can edit and

play48:29

view files

play48:30

so earlier we briefly talked about nano

play48:33

now it's a basic text editor for linux

play48:35

now on this image that we are running

play48:38

we don't have nano so we have to install

play48:40

it do you remember the command for

play48:41

installing nano

play48:43

that is apt install nano

play48:47

all right now we have nano so we can

play48:50

launch it

play48:51

and optionally supply a file name let's

play48:53

say file1.txt

play48:55

so here we have a basic text editor we

play48:57

can type

play48:59

whatever we want

play49:00

and when we're done look down below the

play49:03

shortcut for exiting is control and x so

play49:05

i'm going to press that

play49:07

now it's asking if you want to save the

play49:09

changes so we press yes

play49:11

and here we confirm the file name we can

play49:13

keep the same file or change the file

play49:15

name let's go ahead

play49:17

so now in this directory we have file on

play49:20

the txt beautiful now to see the content

play49:22

of this file we have a few different

play49:24

commands

play49:25

the simplest one is cat and this has

play49:27

nothing to do with cats it's short for

play49:29

concatenate

play49:30

so later i will show you how we can use

play49:32

this command to concatenate or combine

play49:34

multiple files but with cat we can also

play49:37

see the content of a file so if we say

play49:39

cat file1.txt

play49:42

we can see the content of this file now

play49:44

cat is useful if our file is short and

play49:47

fits on one page but if you're dealing

play49:49

with a long file it's better to use the

play49:52

more command let me give you an example

play49:54

so

play49:55

i'm going to use cat to show the content

play49:57

of this file slash etsy slash

play50:00

add user.conf

play50:03

this is a really long file so if i

play50:05

scroll up look

play50:06

we have a lot of text

play50:08

now sometimes you don't want to see all

play50:10

the content in one go you want to scroll

play50:12

down you want to go page by page this is

play50:14

where

play50:15

we use the more command so more

play50:18

etsy slash adduser.conf

play50:21

now down below look

play50:23

you can see more 15 percent so we're

play50:26

seeing 15 percent of this file now if we

play50:28

press space we can go to the next page

play50:31

so now we're at the position 33

play50:34

alternatively we can press enter

play50:36

to go one line at a time now the problem

play50:39

with more is that we can only scroll

play50:41

down we cannot scroll up so to do that

play50:44

we have to use a different command

play50:45

called less

play50:47

so to exit here we press q

play50:49

good

play50:50

now in this image we don't have less so

play50:53

once again we have to manually install

play50:55

it so apt install less so less is a

play50:58

newer command that is supposed to

play51:00

replace more

play51:02

let's go ahead

play51:04

great so let's look at the same file

play51:07

using the less command

play51:09

hcl slash adduser.com

play51:12

so now using the up and down arrows

play51:15

we can scroll down or up

play51:18

so just by pressing up and down arrows

play51:21

we also have space we can go to the next

play51:23

page

play51:24

and enter just like the more command and

play51:27

when we are done we can press q

play51:30

now we have a couple more commands for

play51:31

viewing the content of a file we have

play51:33

head which we can use to display the

play51:36

first few lines so here we can supply an

play51:38

option

play51:39

and say the number of lines we want to

play51:41

see is 5.

play51:42

let's look at the same file

play51:44

so this shows the first five lines of

play51:47

this file

play51:49

similarly we have tail which shows the

play51:51

last few lines so let's apply an option

play51:54

and say we want to look at the last

play51:56

five files

play51:58

that's it

play51:59

so to recap we can use nano to write

play52:02

something to a file we can use cat to

play52:04

view the content of small files

play52:07

less to view the content of long files

play52:09

in an interactive way and head and tail

play52:13

to view the first view or the last few

play52:14

lines of a file

play52:18

[Music]

play52:22

one of the important concepts in linux

play52:24

is the concept of standard input and

play52:26

output so standard input represents the

play52:28

keyboard and standard output represents

play52:31

the screen but we can always change the

play52:33

source of the input or the output this

play52:35

is called redirection let me show you

play52:37

using a few examples so we talked about

play52:40

the cat command to see the content of a

play52:42

file let's say file1.txt

play52:45

when we execute this command this

play52:47

command or this program reads data from

play52:49

this file

play52:50

and prints it on the standard output

play52:52

which is the screen that is why we see

play52:55

the content here but using the

play52:57

redirection operator

play52:59

which is the greater than sign we can

play53:01

redirect the output from the screen to

play53:03

let's say

play53:05

a different file

play53:06

so now cat will read the content from

play53:08

this file and write it to this file

play53:10

take a look

play53:12

so we have file to.txt

play53:15

if we view it

play53:17

we see the exact same content as file

play53:19

one okay now what is the point of this

play53:22

well earlier i told you that we can use

play53:24

the cat command to concatenate or

play53:26

combine multiple files

play53:28

so here we can say cat file1.txt

play53:32

and file2.txt

play53:33

if you press enter cat is going to read

play53:35

the data from both these files and print

play53:38

it on the terminal which is the standard

play53:40

output but once again

play53:42

using the redirection operator we can

play53:44

write the result to a different file

play53:46

combined the txt so this is how we can

play53:49

use this command to combine multiple

play53:51

files now the redirectional operator is

play53:53

not limited to the cad command we can

play53:55

use it pretty much anywhere for example

play53:58

earlier we talked about the echo command

play54:01

if we say echo hello

play54:02

we see the result on the terminal but if

play54:05

we say echo

play54:06

hello to

play54:08

hello.txt now we have a new file here

play54:11

hello.txt which contains

play54:14

hello so if you want to write a single

play54:17

line to a file we don't have to use nano

play54:19

we can use the echo command so echo

play54:22

whatever to whatever file the txt okay

play54:26

now here's a little exercise for you i

play54:28

want you to get a long listing of the

play54:30

files in the etsy directory and write

play54:32

the output to a file

play54:34

so pause the video and work on this for

play54:36

a few seconds

play54:40

so here's the solution

play54:42

to get a long listing we type ls-l

play54:46

then we specify the path

play54:48

the exit directory

play54:49

now instead of printing the result on

play54:51

the terminal we're going to write it to

play54:52

a file called

play54:54

files.txt

play54:56

okay

play54:56

now let's view this file

play54:59

perfect

play55:00

now one more thing before we finish this

play55:02

lesson using the greater than sign we

play55:04

can redirect the standard output but we

play55:07

also have the less than sign to redirect

play55:09

the standard input i personally haven't

play55:11

found many use cases for this so i

play55:13

didn't cover it in this lesson all right

play55:15

that's all about redirection next we're

play55:17

going to talk about searching for text

play55:19

and files

play55:22

hey guys mosh here if you're serious

play55:24

about learning docker i actually have a

play55:26

complete docker course on my coding

play55:28

school codewithmosh.com this tutorial

play55:31

you've been watching is a cut down

play55:32

version of the complete course in the

play55:34

full course you will learn how to run

play55:36

and deploy a full stack application with

play55:38

a front-end back-end and a database our

play55:41

application also includes automated

play55:43

tests so it's a great real-world

play55:45

scenario for learning docker just like

play55:47

my other courses you will also receive a

play55:49

30 day money back guarantee and a

play55:51

certificate of completion you can add

play55:52

your resume in case you are interested

play55:54

you can find the link down below in the

play55:56

description box

play55:59

[Music]

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Docker CourseSoftware DevelopmentFull StackContainersVirtual MachinesProgrammingDevOpsLinux CommandsDeploymentCoding