Docker is a tool that makes it easy to run applications in containers. Containers provide insulation and security as virtual machines, but they are much less because they run in the host machine's system.
As an iOS developer, why do you want to use Docker? To avoid version issues – run versions of operating systems, programming languages, database applications, web apps and web servers, machine learning programs – all in isolated environments, to avoid side effects from anything else you've installed on your Mac. Basically, to join the ranks of my machine developers!
In this tutorial, you will be comfortable with Doctor vocabulary and commands to create, inspect and remove containers, networks and data volumes. You learn to drive Docker containers in the background or foreground, and switch between the two; how to publish ports how to connect a database app and a web app running in separate containers; and how to share directories between containers and your Mac, and among containers. After completing this tutorial, you will be well on your way to Docker ninjadom!
Note : This doctoral education assumes you are willing to enter Unix commands in Terminal. Some familiarity with Unix commands and file paths, database apps, and
localhost ports will be useful.
Click the Download Materials button at the top or bottom of this tutorial to get the project files you want to build the web server program later in this tutorial.  Install Docker
Docker was originally developed for Linux. Running Docks on macOS used to be quite complicated, but a native macOS app Docks for Mac launched in July 2016, so now it's a breeze!
Community Edition (CE) is a free download, so Download Docker CE for Mac, and install and run the app. Moby whale should appear in your Mac status bar:
Docker Terminology & Housekeeping
In this section you will learn some Docker vocabulary and become comfortable using the Unix command line for basic home tasks.
Note : It is a practical list of all the commands in this tutorial – scroll down to the end.
Open Terminal and enter this command to see that Docker is running OK:
Docks run hello world
The docker command for workhorse is
docks run and this is the simplest
docker-run command – it sets the Docker image to run. If the image is not on your host system, it tries to draw it from the standard Docker image register. The meaning of the word image is similar to disk images you download as
.dmg files. A docker image is an app and you run it on your system in a docker container .
The end of this command explains what Docker did just:
Could not find the picture hello-world: newest & local last: Trekker from library / hello world d1725b59e92d: Pull complete Benefit: sha256: 0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788 Status: Downloaded Recent Photo for Hello World: Last Hi from Docker! This message shows that the installation appears to work properly. To generate this message, Docker took the following steps: 1. The docker client contacted the docker demo. 2. The docker demo pulled the hello world image from the Docker Hub. (AMD64) 3. The docker demo created a new container from the image that is running executable that produces the output you are currently reading. 4. The docker demo streamed the one sent to the Docker client who sent it to your terminal.
Wow, many new terms to learn! tag which specifies the version. If you do not request a specific version, Docker assumes you want the latest version.
library / hello-world– the location in the standard image directory Docker Hub . You are visiting this site in the section Running a web app
dockercommand line interface (CLI). The client gives instructions to the demon using the engine's REST API .
Note : You will learn about Docker's network and data volume features later in this tutorial.
There are many types of Docker images:
- OS: Most Linux flavors like Ubuntu, Alpine etc.
- programming languages:: MySQL, CouchDB, PostgreSQL, Oracle, IBM Db2 etc.
- Application Frames: Node .ps, Bottle, Kitura, Tomcat, etc.
- Web Servers: Nginx, Apache, etc.
- Web Apps, including Python Apps and Jupyter Machine Learning Notebooks
A docker image consists of layers – lower layers (OS or programming language ) used by higher layers (API or app). By running an image, a container is created – a thin read-write layer on top of read-only layers of the image. You can run the same image in multiple containers with very little memory. Each container is only read-write-made and only one copy of the image is on the system. The image's top-reading layer specifies the command to run in the container – for
hello world this command just outputs the message Hello from Docker! .
Using Docker Commands
Now to the housekeeping section – you need to keep track of what Docker creates on your system so you can remove things When you do not
Firstly, some general information about the Sync of Docker Command Line Interface (CLI): Docker commands resemble Unix commands, but they start with "docks" like
docker network .
Most commands have more alternatives and many alternatives have shorthand versions. The full name of the option is
- something with two hyphens, like
- name or
- publish . The Shorthand version is
- publish or
. A few alternatives, such as
- name have no shorthand version.
Most choices require values such as
-p 8080: 8080 or
- name kitura . Some have no values and can be run together, like
-ti short for
- interactive - twenty . You must enter an option value just after the option name, but alternatives and selection parameters can be displayed in any order.
Many alternative values map something on the host machine to something in the container. In your case, your host is your Mac.
You will use all these and more in this tutorial.
Start by entering this command in the terminal window:
The output shows the Docker pictures on your system, especially Hello world: last Docker photo taken:
REPOSITORY TAG IMAGE ID CREATED SIZE hey-world last e38bc07ac18e 2 months ago 1,85kB
Note : Your output will display different IMAGE ID and CREATED values.
When the end of
Docker Run Hello World says, Docker daemon created a container from this image to drive the driveability that gave the exit.
Run this command to show everyone (
-a ) Dockers on your system:
dolls ps -a
Note : This does the same as the
docker ls -a . For both commands, the option
-a leaves only containers that are running.
The output shows only one container:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTNAMMER 4ed31ad50912 hello world "/ hey" 16 sec ... Finished ... stupefied_gates
Docks created an ID and a name on the container - your values will be different. Even though the container is finished, it is still on the system.
hello world and then show all the containers left:
docks run hello world docks ps -a
Note : Use Arrow Up and Scroll down to navigate through the Unix commands you have run in this terminal window.
Now there is another container with different ID and name values:CONTAINER ID IMAGE ... NAMES 4ed31ad50912 hello world ... stupefied_gates e5d3669f5ca1 hello world ... flamboyant_zhukovsky
You can remove a container by specifying the name or ID or only the 3 first characters in its ID and you can remove multiple containers in a single command such as (your ID and name will be different) :  docker rm e5d stupefied_gates
The output only echoes the container IDs or names. Confirm that the containers are gone:dolls ps -a
It's fun to see the names that Docker comes up with, but when you're going to work with a container for a while, it's convenient to give your own name to the container. Enter this command:docks run - name heiWorld hello world
Then set up the container:dolls ps -a
And you've called your container:CONTAINER ID IMAGE ... NAMES c5f411a593a3 hello world ... helloWorld
Run the same command again:docks run - name heiWorld hello world
You will receive an error message because the name of the container is already in use:Docks: Wrong response from the demon: Conflict. The container name "/ helloWorld" is already in use by container "C5f411a593a341593ff531c444c44f7dd7fd3f1a006395c9c3cbf5ff687838e1". You must remove (or rename) that container to reuse that name.
Next, I want to show you a cool housekeeping trick, so run
dolls run hello-worldmultiple times to get many finished containers that set up your system. Then run this command to list them:docks ps -a -q -f status = completed
This is the
docker ps-acommand you used to display all Docker containers on your system plus two options. Alternate
-qis short for
- setso the command shows only the numeric IDs. Alternate
-fis short for
- Filterand the filter state is
Status = Completed. So the output looks like this (your IDs will be different):d8d962602abf 64b2eb1af5da 5fde263a26a0 1659b24f2ce2 ff5e7f6a17b5 ab4bf3b4c32b 0d0e48dfcf32 18d8beb2fe60 c5f411a593a3
Now, instead of copying each ID in the command
dock rmyou can only feed this output to the command
docks rm:docks rm $ (docks ps - a -q -f status = completed)
This command first runs the section in
$ ()to get the list of finished container IDs and removes them all - brilliant!
Now that you have removed the containers, you can also remove the image:docks rmi hello-world
Your output looks like this:Untagged: hello world: last Untagged: hello world @ sha256: f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d 59bde73361e77 Deleted: sha256: e38bc07ac18ee64e6d59cf2eafcdddf9cec2364dfe129fe0af75f1b0194e0c96 Deleted: sha256: 2b8cbd0846c5aeaa7265323e7cf085779eaf244ccbdd982c4931aef9be0d2faf
Look for pictures again:dock pictures
And your system is now clean:REPOSITORY TAG IMAGE ID CREATED SIZE
Time Savings Tips - Docker Cleanup Terminal Window : Move this terminal window to a corner of your desktop and use it only to run Docs Cleanup Commands. In this way, the command you want will be just a few Arrow up keystrokes away. Another trick is to enter the UNIX command
storyto find the number of the command you want, and then run the command again by entering
!. For example, below are the last commands in my story, then enter the command
! 18removes all
exitedcontainers.15 docks run - name heiWorld hello world 16 dolls ps -a 17 docks ps -a -q -f status = completed 18 docks rm $ (docks ps -a -q -f status = closed) 19 docks rmi hello world 20 puppets 21 story
Running a Web App in a Docker
Many iOS apps communicate with a web server, which is also the backend of a web app. And you will often have interactions with the iOS app that will be reflected in the web app, and vice versa. You can run the web application locally without containers, but running it in containers can make it easier to test different configurations in isolation, or to test user types with different privileges.
To run a web app in a container, be the first You must learn how to access the container ports in the browser's
local host. The docking period is publishing ports . In this section you will learn about publishing ports, and you can also look at the regular Docker image-registered Docker Hub.
Actually, you do not need artificial intelligence to find out ports! The syntax is
- publishfollowed by two port numbers, separated by a colon. The trick is to remember the first value is for the host here and the other value is for the container there - you publish a port here from there ]. Firstly, the hostport - lets you specify any number you like, but the easiest option is to use the same number as the container's port. For the second number, you get stuck with what the container is exposed to - you get an error if you try to change it. The ability to publish the container's port to different
localhostports makes it easy to run multiple web applets at the same time.
Start with a very simple web app from IBM Kitura. In a browser, load the Docker Hub Default Docker Dock. Enter kitura in the field then select the first returned item:
Open a new terminal window - this will be your Docker Race terminal window. Sometimes you run a process in the foreground in this window, so you can not run any household commands in it. This is where the window Docker Cleansing will be useful!
Click Docker Pull Command and paste it into Docks run terminal window:docks pull ibmcom / kitura-ubuntu
ibmcom / kitura-ubuntucarries a simple web app on the Kitura application frame, stored on the
swift-ubuntuimage, which is the swift programming language layered on top of the Ubuntu operating system . Ubuntu is a version of Linux.
The Picture Docks Hub page does not provide any specific race descriptions, so keep going and try it:docks run ibmcom / kitura-ubuntu
Note: : You could have just entered the command
docks run ibmcom / kitura-ubuntuinstead of the pull command, and it would have pulled the Docker Hub image before driving. But sometimes you can just download an image and run it later - maybe after you have configured other components.
The container starts with these messages:[2018-10-02T21:53:57.690Z] [WARNING] [ConfigurationManager.swift:261 load(url: deserializerName:)] Can not load data from URL /Kitura-Starter/config/mappings.json [2018-10-02T21:53:57.698Z] [INFO] [main.swift:28 Kitura_Starter] Server will be started at & # 39; http: // localhost: 8080 & # 39 ;. [2018-10-02T21:53:57.703Z] [INFO] [HTTPServer.swift:124 listen(on:)] Ning at port 8080
Note : You can also view a container's ports in the PORTS column of the
It tried, but failed to load data from a JSON file, and started listening to
localhost: 8080. Note that you did not get the Unix shell query back - this process is running in the foreground and has not expired.
Open a browser at localhost: 8080 - nope, "Can not connect to server". What's up? The container discloses port 8080 in its own environment but you must publish this port to a port on the host system to see what's there.
First, get rid of this recovery container: In the Docker Cleaning window, run a command to find the container ID, stop the container, and then remove it. Because there is only one container running, you can use a two-level command of
$ ()nesting that stops and removes all drive containers. Try to do this on your own before checking the solution below.
docker rm $ (docker stop $ (docks ps -q))
This command stops and removes all runners. If you have running containers you want to continue to drive, use
docks pson your own to find the ID of the running container you want to stop and remove. [/spoiler]
Return to ] Docks run terminal window - Unix shell prompt is returned. Enter this command:docks run -p 80: 8080 - name kitura -d ibmcom / kitura-ubuntu