Custom Docker Image
docker-->Ubuntu-->image
Yes, you can absolutely do this. This method allows you to start from a basic system image, manually configure the environment inside the container, and then export the configured environment as a new Docker image. This approach is very useful when you need customized images or specific environment configurations. Here are the basic steps to complete this process:
- Create and Start a Basic Ubuntu Container
First, you need to pull a basic Ubuntu image from Docker Hub and start a container. If you don't have the Ubuntu image yet, you can use the following command to get it:
docker pull ubuntu:latest
Then, start a container based on that image:
docker run -it --name ubuntu_base ubuntu:latest /bin/bash
Here, the -it option makes the container's terminal interactive, --name is used to specify the container's name, here named my_ubuntu_container. /bin/bash starts a Bash session so you can execute commands inside the container.
- Configure the Environment Inside the Container
After the container starts, you will enter Ubuntu's command line interface. Here, you can install the required packages, modify configuration files, install scripts, etc., just like operating on a standard Ubuntu system. For example, you can use apt-get to install packages:
apt-get update
apt-get install -y nginx
After completing all required configurations, you can exit the container by typing the exit command or using Ctrl+D.
- Create a New Image from the Configured Container
Once you have completed all configurations inside the container, you can use the docker commit command to create a new image from that container. Here is the basic format of the command:
docker commit [container ID or name] [new image name]:[tag]
For example, to create a new image named my_custom_ubuntu from the container my_ubuntu_container you previously created and configured, you can use:
docker commit ubuntu_llama_factory ubuntu_base:latest
- Export the Newly Created Image
Now you have a new Docker image containing all custom configurations. You can use the docker save command to export it to a file, as previously mentioned:
docker save -o ubuntu_base.tar ubuntu_base:latest
This way, you have successfully started from a basic Ubuntu image, manually configured the environment, and exported the configured environment as a new Docker image. This image can now be imported into other machines or used as a base for future containers.
This method provides extreme flexibility, allowing you to precisely control the content and configuration of the image. However, it also requires you to have a deeper understanding of Docker and the configuration of the required environment. For scenarios that require repeatedly creating the same environment, consider using a Dockerfile to automate the build process, which may be more efficient.
Copy Files from Local to Docker
docker cp /root/llama_factory.zip singularity_llama_factory:/miniconda3/envs
Import a Tar File into Docker
Use the docker load command and specify the path to the .tar file to import the image:
docker load --input /path/to/your/image.tar
Re-enter the Container
To re-enter the command line interface of a running Docker container, you can use the docker exec command. This command allows you to start a new command or interactive session inside a running container. If you want to get an interactive shell session, you can follow these steps:
- Determine the Container ID or Name
First, you need to know the ID or name of the container you want to enter. You can list all running containers by running the following command to find the ID or name of the container you need to enter:
docker ps
This command will display all active containers, including their ID, created command, creation time, status, ports, and other information. Find the ID or name of the container you want to enter from the output.
- Use the
docker execCommand to Enter the Container
Once you know the container's ID or name, you can use the docker exec command with the -it option (i for interactive, t for allocating a pseudo-terminal) to start an interactive bash session. Assuming your container ID or name is container_id_or_name, you can run:
docker exec -it container_id_or_name bash
If the container doesn't have bash inside, you can try using sh instead:
docker exec -it container_id_or_name sh
This will start a new shell session, allowing you to execute commands inside the container.
- Exit the Container
When you are done and want to exit the container, you can simply type the exit command or use the Ctrl-D shortcut. This will end your shell session but will not stop the container.
Dockerfile
Basic Ubuntu image:
# Use Ubuntu's latest LTS version as the base image
FROM ubuntu:22.04
# Set environment variables to prevent interactive prompts during installation
ENV DEBIAN_FRONTEND=noninteractive
# Update package list
RUN apt-get update
# Install basic tools
RUN apt-get install -y --no-install-recommends \
git \
curl \
vim \
build-essential \
wget
# Clean cache to reduce image size
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Create a new user `developer`
RUN useradd -m developer
# Switch to the new user `developer`
USER root
# Set the working directory to the new user's home directory
WORKDIR /root
RUN echo 'root:YOURPASSWORD' | chpasswd
# Set the command to execute when the container starts
CMD ["/bin/bash"]
If you don't set a user password when building the Docker image, the root user usually won't have a password either. In a Docker container, by default, you can execute commands directly as the root user without a password.
If you started the container via docker run and didn't specify to run as a non-root user, you should already be running as the root user inside the container. You can check the current user by running the whoami command.
If you find that you are not the root user and the su - command asks you for a password but you don't know what it is, you can try the following methods:
-
Restart the container and log in as the root user:
When you use the
docker runcommand, you can specify to run the container as the root user with the-uor--useroption:docker run -u root -it <image_name> /bin/bashThis will allow you to log in to the container as the root user without being asked for a password.
-
Modify the Dockerfile:
If you have access to the Dockerfile, you can modify it to ensure the container runs as the root user:
FROM ubuntu:22.04
# Other instructions...
USER rootThen rebuild the image and start a new container:
docker build -t myimage .
docker run -it myimage -
Create a new container and set the root password:
If you need to set a root password in the container, you can add the following instruction to the Dockerfile:
FROM ubuntu:22.04
RUN echo 'root:YOURPASSWORD' | chpasswdReplace
YOURPASSWORDwith the password you want to set. Then rebuild the image.
Please note that setting the root password directly in the Dockerfile is not a secure practice, as the password will exist in plaintext in the image. This practice should only be used in environments you fully control and should not be used in production environments. In production environments, you should use more secure methods to manage credentials.
Things to Install in Docker Ubuntu
# Switch to bash
bash
apt-get update
# Install basic tools
apt-get install -y vim git build-essential wget
# Install ifconfig and proxy
apt-get install -y net-tools
# Install sudo
apt-get install -y sudo
# Install ping
apt install -y vim git build-essential wget
sudo apt-get update && sudo apt-get install -y \
build-essential \
libssl-dev \
uuid-dev \
libgpgme11-dev \
squashfs-tools \
libseccomp-dev \
wget \
pkg-config \
git \
cryptsetup-bin