Get the containerID, docker - docker

I've build a container using
$docker-compose build
How do I get the container's ID ? I tried this, but just gave me the name and not container's ID :
$docker-compose ps

You build an image, and you run a container from an image.
Here I believe you are mixing both concepts: the output of your build command is an image (which you can see using docker images), docker ps will show you the running containers.

Related

Commands are not working in Ubuntu container

I have created a container using the following command: docker container run -i ubuntu. However, when I try to run a command within the container, such as cd, I get the following error: bash: line 1: cd: $'bin\r': No such file or directory. What could be the issue?
When you docker run an image, or use an image in a Dockerfile FROM line, or name an image: in a Docker Compose setup, Docker first checks to see if you have that image locally. If you have that image, Docker just uses it without checking Docker Hub or the other upstream registry.
Meanwhile, you can docker build or docker tag an image with any name you want...even a name that matches an official Docker Hub image.
You mention in a comment that you at some point did run docker build -t ubuntu .... That replaces the ubuntu image with what you built, so when you later docker run ubuntu, it's running your modified image and not the official Docker Hub Ubuntu image.
This is straightforward to fix. If you
docker rmi ubuntu
it will delete your local (modified) copy, and the next time you use it, Docker will automatically pull it from Docker Hub. It should also work to
# Explicitly get the Docker Hub copy of the image
docker pull ubuntu
# Build a custom image, pulling whatever's in the FROM line
docker build --pull -t my/image .
(You can also hit this in a Docker Compose setup if you specify both image: and build:; this instructs Compose on an explicit name to use for the built image. You do not need to repeat the FROM line in image:, and it causes trouble if you do. The resolution is the same as described above. I might leave image: out entirely unless you're planning to push the image to a registry.)

Docker pull hello-world showing successful create but when using docker ps or docker ps -a not shoing images

I used docker ps/docker ps -a/docker ps -n 1 all not showing my first image.
But it after I using docker pull hello-world it saying it installed successfully
docker pull pulls an image (and all the layers that make it up) to your local machine, but doesn't run anything.
docker ps lists containers on your system.
Once you run that container (using docker run hello-world), you'll see it in dokcer ps.
To view the image you pulled, you could use docker images.
As you find from the previous answer docker pull will download the image (mostly from the docker hub) and when trying to pull next time, it finds the image already in your local machine. To see all the images you have locally, use docker image ls.

Docker --tag vs --name clarification

I'm pretty new to docker and I'm a bit puzzled by the difference between tagging (--tag) an image and assigning it a name (--name).
For example, I can see that if I build my custom image out of a Docker file, I can tag it with a name:
sudo docker build --tag=tomcat-admin .
sudo docker run -it tomcat-admin
Passing the name to docker inspect produces a result:
docker inspect tomcat-admin
However it doesn't contain the same attributes of a "named" image:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' tomcat-admin
Template parsing error: template: :1:19: executing "" at <.NetworkSettings.IPA...>: map has no entry for key "NetworkSettings
"
Somebody to shed some light on it?
Thanks!
I think you mixed two concepts here, which causes the confusion. On the one hand there is a Docker image which you can think of as a blueprint for starting a container. On the other hand there are containers which are running instances that are based on an image.
When you docker build -t tagname . you are creating an image and tag it with a "name:tag" format usually. So for example, you are building your image as
docker build -t myimage:1.0 .
which creates a new image that is named myimage with a version of 1.0. This is what you will see when you run docker images.
The --name parameter is then used when you create and start a new container based of your image. So for example, you run a new container using the following command:
docker run -it --name mycontainerinstance myimage
This creates a new container based of your image myimage. This container instance is named mycontainerinstance. You can see this when you run docker ps -a which will list the container with its container name mycontainerinstance.
So to better understand the difference, have a look at the docs for building an image and running a container, specifying an image. When reading the docs you will notice which commands target an image and which commands are for containers. You will also see, that there are commands that work for images and containers like docker inspect does.
Inspecting for a network address of course only works when you provide a container name, not an image. In your special case, the container got a generated name, which you can see by running docker ps -a. When you provide this name to the docker inspect command, you will likely see the ip address you wanted.
You tag an image
docker build --tag=tomcat-admin .
but you assign a name to a container
docker run -it tomcat-admin
You can assign multiple tags to images, e.g.
docker build --tag=tomcat-admin --tag=tomcat-admin:1.0 .
If you list images you get one line per tag, but they are related to the same image id:
docker images |grep tomcat
tomcat-admin 1.0 955395353827 11 minutes ago 188 MB
tomcat-admin latest 955395353827 11 minutes ago 188 MB
You can tag images also a second time, not just when you build them, so you can keep different image versions.
When you run a container based on a specific image, you can assign it a name, so you can refer it using the name instead than using the containerId.
Obviously you get different attributes by inspecting images and containers. I think it's more clear if you use different name for image tag and container name, e.g.
docker build --tag=tomcat-admin .
docker run -d -ti --name=tomcat-admin-container tomcat-admin
docker inspect tomcat-admin ==> You inspect the image
docker inspect tomcat-admin-container ==> You inspect the container
The confusing thing is that a tag consists of a name and a tag. In documentation you can see that:
--tag , -t Name and optionally a tag in the ‘name:tag’ format
So if you omit the :tag part, you actually add a name for the image. That's it.
The difference between image names and container names is explained in other's answers.

What's the difference between the docker commands: run, build, and create

I see there are three docker commands that seem to do very similar things:
docker build
docker create
docker run
What are the differences between these commands?
docker build builds a new image from the source code.
docker create creates a writeable container from the image and prepares it for running.
docker run creates the container (same as docker create) and runs it.
docker build . converts your Dockerfile into an image.
docker create your-image creates a container from your image from step 1.
docker start container_id starts the container from step 2.
docker run image is a shortcut for 2. and 3. (docker create image and
docker start container_id).
Here is the difference between image and container:
Image
An image is a specified snapshot of your filesystem and includes the starting command of your container. An image occupies just disk-space, it does not occupy memory/cpu. To create an image you usually create instructions how to build that image in aDockerfile. FROM and RUN commands in the docker file create the file-snapshot. One may build an image from a docker file with docker build <dockerfile>
Container
You can create new containers with an image. Each container has a file-snapshot which is based on the file-snapshot created by the image. If you start a container it will run the command you specified in your docker file CMD and will use part of your memory and cpu. You can start or stop a container. If you create a container, its not started by default. This means you can't communicate to the container via ports etc. You have to start it first. One may create an container from an image by docker create <image>. When a container has been created it shows the id in the terminal. One may start it with docker start <container_id>.

running incorrect docker image osx

I created a new image running:
docker build -t team1/es-image2 . | tee build.log
First, the create date doesn't reflect today's date. I wasn't concerned with that at first but after running it, it sort of makes sense...the running image is from another image created previously. I ran it with this command:
docker run -i -t --rm -P team1/es-image2
I verified that the correct image was running using:
docker ps
I deleted the older image and tried running again but it still appears to be running the older image because -P showed all the older mapped ports and the working directory was also from the older image.
So, I can't understand why, the build is using the older containers even though the Dockerfile is not specifying all the items that were specified in the older image.
Thanks!
docker ps
is only to show container.
To show images you need to use
docker images
And to delete them use
docker rmi
A little clarification about image and container.
An image is the definition of a container, and a container is a part of the system isolated from the current directory tree.
You use an image to run a container. You can use the same image to run multiple container.
When building the image from the Dockerfile, you may specify --no-cache=true to exclude any intermediate builds.

Resources