How do you build a docker container from an image? - docker

I have a mysql docker image w/ id: fb86ef4dd8b7
What is the command to build a container and name it using the above docker-image: MY_NEW_CONTAINER
"docker build -t MY_NEW_CONTAINER..." but how do I specify to use the above docker image?

A container is a running copy of an image. So to create a container from an inage, you simply docker run it.
You can give the container a name (instead of the auto-generated scientist name) with the --name option.
docker run --name mysql_container_1 fb86ef4dd8b7
Why doesn't your image have a name? You should docker tag it.

Related

How to duplicate running docker container

I have a docker container running
> docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5a24953e383 gradle "bash" 22 minutes ago Up 22 minutes # naughty_torvalds
Can I duplicate this running container and run it? What is the command for it?
You can create a new image from that container using the docker commit command:
docker commit c5a24953e383 newimagename
And then start a new container from that image:
docker run [...same arguments as the other one...] newimagename
You can use:
docker run --name duplicateImage --volumes-from Image -d -p 3000:80 nginix:latest
The --volumes-from Image duplicates the 'Image' container.
So you will now have a container named Image and a container named duplicateImage and they will contain the same image that is running (a container).

How to push docker images with the associated containers

I have a docker container in a virtual machines where I am hosting my Postgres database, but when I pull that image to my local machine the container does not show up. I have tried import/export and save/load but still I can't get the container to show in my local machine. Any help will be much appreciated!
You have to build containers after pulling in the images.
The simplest way is
docker create --name my_container bf141206f773
where bf141206f773 is the image's hash. You can also use its full_name:tag.
To start your new container:
docker start my_container
To enter your new container:
docker exec -it my_container /bin/bash
If you want to see how I do this in a deployment-environment, check out my Laravel QuickStart project's docker files: https://github.com/phpexpertsinc/laravel_quickstart

Possible to retrieve file from Docker image that is not a container?

I have created an application that uses docker. I have built an image like so: docker build -t myapp .
While in my image (using docker run -it myapp /bin/bash to access), a image file is created.
I would like to obtain that file to view on my local as I have found out that viewing images on Docker is a complex procedure.
I tried the following: docker cp myapp:/result.png ./ based on suggestions seen on the webs, but I get the following error: Error response from daemon: No such container: myapp
Image name != container name
myapp is the name of the image, which is not a running container.
When you use docker run, you are creating a container which is based on the myapp image. It will be assigned an ID, which you can see with docker ps. Example:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa58c8ff2f34 portainer/portainer "/portainer" 4 months ago Up 5 days 0.0.0.0:9909->9000/tcp portainer_portainer_1
Here you can see a container based on the portainer/portainer image. It has the ID aa58c8ff2f34.
Once you have the ID of your container, you can pass it to docker cp to copy your file.
Specifying the container name
Another approach, which may be preferable if you are automating / scripting something, is to specify the name of the container instead of having to look it up.
docker run -it --name mycontainer myapp /bin/bash
This will create a container named mycontainer. You can then supply that name to docker cp or other commands. Note that your container still has an ID like in the above example, but you can also use this name to refer to it.
You could map a local folder to a volume in the image, and then copy the file out of the image that way.
docker run -it -v /place/to/save/file:/store myapp /bin/bash cp /result.png /store/

Inject configuration into volume before Docker container starts

I am looking for a way to create a Docker volume and put some data on it just before a specific container is started - which needs the configuration on startup.
I do not want to modify the container. I would like to use a vanilla container straight from the Docker Hub.
Any ideas?
Update
I did not mention that all this has to be done in a compose file. If I would do it manually, I could wait for the configuration injecting container to finish.
Absolutely! Just create your volume beforehand, attach it to any container (A base OS like Ubuntu would work great), add your data, and you're good to go!
Create the volume:
docker volume create test_volume
Attach it to an instance where you can add data:
docker run --rm -it --name ubuntu_1 -v test_volume:/app ubuntu /bin/sh
Add some data:
Do this within the container; which you are in from the previous command.
touch /app/my_file
Exit the container:
exit
Attach the volume to your new container:
Of course, replace ubuntu with your real image name.
docker run --rm -it --name ubuntu_2 -v test_volume:/app ubuntu /bin/sh
Verify the data is there:
~> ls app/
my_file

Start up docker container without dockerfile

I've been using Dockerfiles so often that I've forgotten how to start up a new one without one.
I was reading https://docs.docker.com/engine/reference/commandline/start/ and ofc it doesn't state how to start up a new one.
docker run -it ubuntu:16.04 bash
A Dockerfile describes a Docker image not a container.
The container is an instance of this image.
If you want to run a container without building an image (which means without creating a Dockerfile), you need to use an existing image on the Docker Hub (link here).
N.B.: The Docker Hub is a Docker online repository, they are more repositories like Quay, Rancher and others.
For example, if you want to test this, you can use the hello-world image found on the Docker Hub: https://hub.docker.com/_/hello-world/.
According to the documentation, to run a simple hello-world container:
$ docker run hello-world
Source: https://hub.docker.com/_/hello-world/
If you don't have the image locally, Docker will automatically pull it
from the web. If you want to manually pull the image you can run the
following command:
$ docker pull hello-world
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Source: https://hub.docker.com/_/hello-world/
docker start is used to start a stopped container which already exists and in stopped state.
If you want to start a new container use docker run instead. For information about docker run please see https://docs.docker.com/engine/reference/commandline/run/

Resources