- In the actual VM which I entered using justincormack/nsenter. Steps to reproduce the behavior. The quickest way to reproduce the problem is: Get an M1 Mac and install Docker for Mac for M1 Macs; Run docker run -it -rm -platform linux/amd64 swift:5.3 bash -c 'echo 'print( 'hello world ')' /tmp/test.swift && cd /tmp && swiftc test.swift.
- Docker Desktop is now available for Apple silicon as well as Intel chips. This enables developers with their choice of local development environments, and extends development pipelines for ARM-based applications. For more information, see Docker Desktop for.
- Docker Desktop is now available for Apple silicon as well as Intel chips. This enables developers with their choice of local development environments, and extends development pipelines for ARM-based applications. For more information, see Docker Desktop for Apple silicon. Bug fixes and minor changes đ.
Article Category:#Code
Posted on
May 27, 2020 There are differences between nsenter and docker exec; namely, nsenter doesn't enter the cgroups, and therefore evades resource limitations. The potential benefit of this would be debugging and external audit, but for remote access, docker exec is the current recommended approach. Jun 05, 2020 When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to.
.Updates
Hello. How are you? Thanks for stopping by.
It used to be tricky to get Docker working on OS X, which is why I wrote this here blog post. With the release of Docker 1.8, it just got way easier. Now there's a new thing, Docker Toolbox, that makes it super easy. You should install that instead of reading this post. Or you can read it if you want, I think it's a pretty good blog post and you'll learn some stuff. No pressure, though.
Have you heard of Docker? You probably haveâeverybodyâs talking about it. Itâs the new hotness. Even my dadâs like, âwhatâs Docker? I saw someone twitter about it on the Facebook. You should call your mom.â
Docker is a program that makes running and managing containers super easy. It has the potential to change all aspects of server-side applications, from development and testing to deployment and scaling. Itâs pretty cool.
Recently, Iâve been working through The Docker Book. Itâs a top notch book and I highly recommend it, but Iâve had some problems running the examples on OS X. After a certain point, the book assumes youâre using Linux and skips some of the extra configuration required to make the examples work on OS X. This isnât the bookâs fault; rather, it speaks to underlying issues with how Docker works on OS X.
This post is a walkthrough of the issues youâll face running Docker on OS X and the workarounds to deal with them. Itâs not meant to be a tutorial on Docker itself, but I encourage you to follow along and type in all the commands. Youâll get a better understanding of how Docker works in general and on OS X specifically. Plus, if you decide to dig deeper into Docker on your Mac, youâll be saved hours of troubleshooting. Donât say I never gave you nothing.
First, letâs talk about how Docker works and why running it on OS X no work so good.
How Docker Works
Docker is a client-server application. The Docker server is a daemon that does all the heavy lifting: building and downloading images, starting and stopping containers, and the like. It exposes a REST API for remote management.
The Docker client is a command line program that communicates with the Docker server using the REST API. You will interact with Docker by using the client to send commands to the server.
The machine running the Docker server is called the Docker host. The host can be any machineâyour laptop, a server in the Cloudâ˘, etcâbut, because Docker uses features only available to Linux, that machine must be running Linux (more specifically, the Linux kernel).
Docker on Linux
Suppose we want to run containers directly on our Linux laptop. Hereâs how it looks:
Docking on Linux
The laptop is running both the client and the server, thus making it the Docker host. Easy.
Docker on OS X
Hereâs the thing about OS X: itâs not Linux. It doesnât have the kernel features required to run Docker containers natively. We still need to have Linux running somewhere.
Enter boot2docker. boot2docker is a âlightweight Linux distribution made specifically to run Docker containers.â Spoiler alert: youâre going to run it in a VM on your Mac.
Hereâs a diagram of how weâll use boot2docker:
Docking on OS X
Weâll run the Docker client natively on OS X, but the Docker server will run inside our boot2docker VM. This also means boot2docker, not OS X, is the Docker host, not OS X.
Make sense? Letâs install dat software.
Installation
Step 1: Install VirtualBox
Go here and do it. You donât need my help with that.
Step 2: Install Docker and boot2docker
You have two choices: the offical package from the Docker site or homebrew. I prefer homebrew because I like to manage my environment from the command line. The choice is yours.
Step 3: Initialize and start boot2docker
First, we need to initialize boot2docker (we only have to do this once):
Next, we can start up the VM. Do like it says:
Step 4: Set the DOCKER_HOST environment variable
The Docker client assumes the Docker host is the current machine. We need to tell it to use our boot2docker VM by setting the DOCKER_HOST
environment variable:
âYour VM might have a different IP addressâuse whatever boot2docker up
told you to use. You probably want to add that environment variable to your shell config.
Step 5: Profit
Letâs test it out:
Great success. To recap: weâve set up a VirtualBox VM running boot2docker. The VM runs the Docker server, and weâre communicating with it using the Docker client on OS X.
Bueno. Letâs do some containers.
Common Problems
We have a âworkingâ Docker installation. Letâs see where it falls apart and how we can fix it.
Problem #1: Port Forwarding
The Problem: Docker forwards ports from the container to the host, which is boot2docker, not OS X.
Letâs start a container running nginx:
This command starts a new container as a daemon (-d
), automatically forwards the ports specified in the image (-P
), gives it the name âwebâ (--name web
), and uses the nginx
image. Our new container has the unique identifier 0092c03e1eba...
.
Verify the container is running:
Under the PORTS heading, we can see our container exposes port 80, and Docker has forwarded this port from the container to a random port, 49153, on the host.
Letâs curl our new site:
It didnât work. Why?
Remember, Docker is mapping port 80 to port 49153 on the Docker host. If we were on Linux, our Docker host would be localhost, but we arenât, so itâs not. Itâs our VM.
The Solution: Use the VMâs IP address.
boot2docker comes with a command to get the IP address of the VM:
Letâs plug that into our curl command:
Success! Sort of. We got the web page, but we got The VMâs Host only interface IP address is:
, too. Whatâs the deal with that nonsense.
Turns out, boot2docker ip
outputs the IP address to standard output and The VM's Host only interface IP address is:
to standard error. The $(boot2docker ip)
subcommand captures standard output but not standard error, which still goes to the terminal. Scumbag boot2docker.
This is annoying. I am annoyed. Hereâs a bash function to fix it:
Stick that in your shell config, then use it like so:
Groovy. This gives us a reference for the IP address in the terminal, but it would be nice to have something similar for other apps, like the browser. Letâs add a dockerhost
entry to the /etc/hosts
file:
Now we can use it everywhere:
Great success. Make sure to stop and remove the container before continuing:
âVirtualBox assigns IP addresses using DHCP, meaning the IP address could change. If youâre only using one VM, it should always get the same IP, but if youâre VMing on the reg, it could change. Fair warning.
Bonus Alternate Solution: Forward all of Dockerâs ports from the VM to localhost.
If you really want to access your Docker containers via localhost, you can forward all of the ports in Dockerâs port range from the VM to localhost. Hereâs a bash script, taken from here, to do that:
By doing this, Docker will forward port 80 to, say, port 49153 on the VM, and VirtualBox will forward port 49153 from the VM to localhost. Soon, inception. You should really just use the VMâs IP address mmkay.
Problem #2: Mounting Volumes
The Problem: Docker mounts volumes from the boot2docker VM, not from OS X.
Docker supports volumes: you can mount a directory from the host into your container. Volumes are one way to give your container access to resources in the outside world. For example, we could start an nginx container that serves files from the host using a volume. Letâs try it out.
First, letâs create a new directory and add an index.html
:
(Make sure to replace /Users/Chris
with your own path).
Next, weâll start another nginx container, this time mounting our new directory inside the container at nginxâs web root:
We need the port number for port 80 on our container:
Letâs try to curl our new page:
Well, that didnât work. The problem, again, is our VM. Docker is trying to mount /Users/Chris/web
from the host into our container, but the host is boot2docker, not OS X. boot2docker doesnât know anything about files on OS X.
The Solution: Mount OS Xâs /Users
directory into the VM.
By mounting /Users
into our VM, boot2docker gains a /Users
volume that points to the same directory on OS X. Referencing /Users/Chris/web
inside boot2docker now points directly to /Users/Chris/web
on OS X, and we can mount any path starting with /Users
into our container. Pretty neat.
boot2docker doesnât support the VirtualBox Guest Additions that allow us to make this work. Fortunately, a very smart person has solved this problem for us with a custom build of boot2docker containing the Guest Additions and the configuration to make this all work. We just have to install it.
First, letâs remove the web container and shut down our VM:
Next, weâll download the custom build:
Finally, we share the /Users
directory with our VM and start it up again:
âReplacing the boot2docker image wonât erase any of the data in your VM, so donât worry about losing any of your containers. Good guy boot2docker.
Letâs try this again:
Great success! Letâs verify that weâre using a volume by creating a new file on OS X and seeing if nginx serves it up:
Sweet damn. Make sure to stop and remove the container:
âIf you update index.html
and curl it, you wonât see your changes. This is because nginx ships with sendfile
turned on, which doesnât play well with VirtualBox. The solution is simpleâturn off sendfile
in the nginx config fileâbut outside the scope of this post.
Problem #3: Getting Inside a Container
The Problem: How do I get in there?
So youâve got your shiny new container running. The ports are forwarding and the volumes are ... voluming. Everythingâs cool, until you realize somethingâs totally uncool. Youâd really like to start a shell in there and poke around.
The Solution: Linux Magic
Enter nsenter. nsenter is a program that allows you to run commands inside a kernel namespace. Since a container is just a process running inside its own kernel namespace, this is exactly what we need to start a shell inside our container. Letâs make it so.
âThis part deals with shells running in three different places. TrĂŠs confusing. Iâll use a different prompt to distinguish each:
>
for OS X$
for the boot2docker VM%
for inside a Docker container
First, letâs SSH into the boot2docker VM:
Next, install nsenter
:
Docker For Mac Download
(How does that install it? jpetazzo/nsenter
is a Docker image configured to build nsenter from source. When we start a container from this image, it builds nsenter and installs it to /target
, which weâve set to be a volume pointing to /var/lib/boot2docker
in our VM.
In other words, we start a prepackaged build environment for nsenter, which compiles and installs it to our VM using a volume. How awesome is that? Seriously, how awesome? Answer me!)
Finally, we need to add /var/lib/boot2docker
to the docker
userâs PATH
inside the VM:
We should now be able to use the installed binary:
Letâs start our nginx container again and see how it works (remember, weâre still SSHâd into our VM):
Time to get inside that thing. nsenter needs the pid of the running container. Letâs get it:
The moment of truth:
Great success! Letâs confirm weâre inside our container by listing the running processes (we have to install ps
first):
Docker Nsenter
We can see two nginx processes, our shell, and ps. How cool is that?
Getting the pid and feeding it to nsenter
is kind of a pain. jpetazzo/nsenter includes docker-enter, a shell script that does it for you:
The default command is sh
, but we can run any command we want by passing it as arguments:
This is totally awesome. It would be more totally awesomer if we could do it directly from OS X. jpetazzoâs got us covered there, too (that guy thinks of everything), with a bash script we can install on OS X. Below is the same script, but with a minor change to default to bash, because thatâs how I roll.
Just stick this bro anywhere in your OS X PATH (and chmod +x
it, natch) and youâre all set:
Letâs test it out:
Yes. YES. Cue guitar solo.
Donât forget to stop and remove your container (nag nag nag):
The End
You now have a Docker environment running on OS X that does all the things youâd expect. Youâve also hopefully learned a little about how Docker works and how to use it. Weâve had some laughs, and weâve learned a lot, too. Iâm glad weâre friends.
If youâre ready to learn more about Docker, check out The Docker Book. I canât recommend it enough. Throw some money at that guy.
The Future Soon
Docker might be the new kid on the block, but weâre already thinking about ways to add it to our workflow. Stay tuned for great justice.
Docker For Mac Center Contact
Was this post helpful? How are you using Docker? Let me know down there in the comments box. Have a great. Call your mom.
Docker For Mac Center For Engineering
Estimated reading time: 3 minutes
Docker Desktop stores Linux containers and images in a single, large âdisk imageâ file in the Mac filesystem. This is different from Docker on Linux, which usually stores containers and images in the /var/lib/docker
directory.
Where is the disk image file?
To locate the disk image file, select the Docker icon and thenPreferences > Resources > Advanced.
The Advanced tab displays the location of the disk image. It also displays the maximum size of the disk image and the actual space the disk image is consuming. Note that other tools might display space usage of the file in terms of the maximum file size, and not the actual file size.
If the file is too big
If the disk image file is too big, you can:
- move it to a bigger drive,
- delete unnecessary containers and images, or
- reduce the maximum allowable size of the file.
Move the file to a bigger drive
To move the disk image file to a different location:
Select Preferences > Resources > Advanced.
In the Disk image location section, click Browse and choose a new location for the disk image.
Click Apply & Restart for the changes to take effect.
Do not move the file directly in Finder as this can cause Docker Desktop to lose track of the file.
Delete unnecessary containers and images
Check whether you have any unnecessary containers and images. If your client and daemon API are running version 1.25 or later (use the docker version
command on the client to check your client and daemon API versions), you can see the detailed space usage information by running:
Alternatively, to list images, run:
and then, to list containers, run:
If there are lots of redundant objects, run the command:
This command removes all stopped containers, unused networks, dangling images, and build cache.
It might take a few minutes to reclaim space on the host depending on the format of the disk image file:
- If the file is named
Docker.raw
: space on the host should be reclaimed within a few seconds. - If the file is named
Docker.qcow2
: space will be freed by a background process after a few minutes.
Space is only freed when images are deleted. Space is not freed automatically when files are deleted inside running containers. To trigger a space reclamation at any point, run the command:
Note that many tools report the maximum file size, not the actual file size.To query the actual size of the file on the host from a terminal, run:
In this example, the actual size of the disk is 2333548
KB, whereas the maximum size of the disk is 64
GB.
Docker For Mac Center Address
Reduce the maximum size of the file
To reduce the maximum size of the disk image file:
Select the Docker icon and then select Preferences > Resources > Advanced.
The Disk image size section contains a slider that allows you to change the maximum size of the disk image. Adjust the slider to set a lower limit.
Click Apply & Restart.
When you reduce the maximum size, the current disk image file is deleted, and therefore, all containers and images will be lost.
mac, disk