How to connect to docker image on mac os
I am installing docker on mac os. I know that there is a docker-machine installed and running between my mac os and the docker images. When I start one docker image, I can’t ping the image from my mac os, instead I have to connect to these images from the docker-machine. I wonder whether there is a way to allow me to connect to the docker images from the mac os.
2 Solutions collect form web for “How to connect to docker image on mac os”
This is typically done by exposing ports to the docker-machine. For example, you might map the SSH port like this:
docker run -d -p 2022:22 my-image
This will bind port 2022 on the docker-machine to port 22 inside the container. Now you can gain access by using the docker-machine’s IP, like this (assuming
ssh -p 2022 user@$(docker-machine ip default)
Of course, this requires your container to expose a service on port 22. You’ll have to refer to your container’s documentation to see what services are being exposed.
If desired, you can also configure access to the services via
localhost, rather than using the docker-machine IP. Doing so would require some networking config in your docker-machine driver. Assuming you are using VirtualBox (the default driver for Mac OS X installs), you can configure port forwarding in the VirtualBox UI or via
VBoxManage (documentation link below).
Docker documentation for exposing ports: https://docs.docker.com/engine/reference/run/#expose-incoming-ports
VirtualBox documentation for port forwarding:
This is the way that I understand it:
A docker-machine is like a virtual machine. A container is based off of the virtual machine (docker-machine), but is completely isolated from other containers (it has it’s own file system, process trees, etc). An image is just a set of layers of containers that have been saved together. So the word “image” and the word “container” could get inter-mingled in a few contexts. Container usually refers to the top-most layer of an image (and is the “write” layer).
The reason I address this is because from your question, I’m not entirely sure what terminology you are trying to use.
You can SSH into both the docker-machine image as well as the containers.
To SSH into a running container, you can use
docker exec -it CONTAINER_NAME bash
To SSH into a running docker-machine image, you can use
docker-machine ssh DOCKER_IMAGE_NAME
I can think of only a few reasons why you would want to SSH into a docker-machine, but it is possible with the command above. Almost always, you will be working with docker containers. That’s the whole reason for Docker in the first place – a container management system.